Deleting last n lines from file using batch file

后端 未结 4 1857
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 09:47

How to delete last n lines from file using batch script

I don\'t have any idea about batch files, I am writing batch file for the first time.

How should I wr

相关标签:
4条回答
  • 2020-12-10 10:03

    This the complete script for remove last N line

    • count the total line
    • set Line = Line - N , remain just processing lines number
    @echo OFF
    setlocal EnableDelayedExpansion
    
    set LINES=0
    for /f "delims==" %%I in (infile.txt) do (
        set /a LINES=LINES+1    
    )
    
    echo Total Lines : %LINES%
    echo.
    
    :: n = 5 , last 5 line will ignore 
    set /a LINES=LINES-5
    
    call:PrintFirstNLine > output.txt
    
    goto EOF
    
    :PrintFirstNLine
    set cur=0
    for /f "delims==" %%I in (infile.txt) do (      
        echo %%I        
        ::echo !cur! : %%I      
        set /a cur=cur+1    
        if "!cur!"=="%LINES%" goto EOF
    ) 
    
    :EOF 
    
    exit /b
    

    Here call:PrintFirstNLine > output.txt will give the output in an external file name as output.txt

    Output for sample Input

    <Project_Name>      
    <CBA_Notifier>      
        <Common>        
        <File>D:\CBA\CBA_Notifier\Project_Name\IPS-Util.jar</File>      
        <File>D:\CBA\CBA_Notifier\Project_Name\Notifier.bat</File>      
        <File>D:\CBA\CBA_Notifier\Project_Name\Notifier.xml</File>      
        <File>D:\CBA\CBA_Notifier\Project_Name\Notifier.jar</File>              
        </Common>       
        <Project_Name>      
            <File>D:\CBA\CBA_Notifier\IPS-Util.bat</File>   
    

    remove last 5 line

    Update

    :PrintFirstNLine
    set cur=0
    for /F "tokens=1* delims=]" %%I in ('type "infile.txt" ^| find /V /N ""') do (
       if "%%J"=="" (echo.) else (
            echo.%%J
            set /a cur=cur+1    
            )  
    
       if "!cur!"=="%LINES%" goto EOF
    )
    
    0 讨论(0)
  • 2020-12-10 10:11

    This script will takes 1 arguement, the file to be trunkated, creates a temporary file and then replaces the original file with the shorter one.

    @echo off
    setlocal enabledelayedexpansion
    set count=
    for /f %%x in ('type %1 ^| find /c /v ""') do set /a lines=%%x-5
    copy /y nul %tmp%\tmp.zzz > nul
    for /f "tokens=*" %%x in ('type %1 ^| find /v ""') do (
      set /a count=count+1
      if !count! leq %lines% echo %%x>>%tmp%\tmp.zzz
    )
    move /y %tmp%\tmp.zzz %1 > nul
    

    If the original file is 5 or less lines, the main output routine will noT create a file. To combat this, I use the copy /y null to create a zero byte file.

    If you would rather not have an empty file, just remove the copy /y nul line, and replace it with the following line:

    if %lines% leq 0 del %1
    

    You should use one method or the other, otherwise source files with 5 or less lines will remain untouched. (Neither replaced or deleted.)

    0 讨论(0)
  • 2020-12-10 10:17

    to delete last lines from your file,

    1 copy starting lines that are needed from file like from- e:\original.txt 2 paste them in new file like- e:\new\newfile1.txt

    code is thanks to the person giving me this code: remember all may be done if you have motive and even blood hb =6. but help of nature is required always as you are a part of it

    @echo off & setLocal enableDELAYedeXpansion set N= for /f "tokens=* delims= " %%a in (e:\4.txt) do ( set /a N+=1 if !N! gtr 264 goto :did

    e:\new4.txt echo.%%a ) :did

    if you have 800 files then use excel to make code for 800 and then copy it to notepad and using Ctrl+h replace space with no space. then rename file as haha.bat . run in folder with files numbered 1.txt 2.txt 3.txt etc. any enquirers welcome Erkamaldev@gmail.com " Long Live Bharata"

    0 讨论(0)
  • A slow method with less coding:

    set input=file.txt
    set remove=7
    
    for /f "delims=" %i in ('find /c /v "" ^< "%cd%\%input%"') do set lines=%i
    set /a lines-=remove
    for /l %i in (1,1,!lines!) do findstr /n . log.txt | findstr /b %i:
    

    May be redirected to a file.
    Each line is prefixed with the line number; may be removed with extra coding.
    A faster version with /g flag in my answer at:
    How to split large text file in windows?

    Tested in Win 10 CMD, on 577KB file, 7669 lines.

    0 讨论(0)
提交回复
热议问题