Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: delete first n lines from a file  (Read 22333 times)

0 Members and 1 Guest are viewing this topic.

babhuko

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    delete first n lines from a file
    « on: February 08, 2012, 10:19:28 PM »
    hi all,

    can someone help me in writing a batch program, which deletes first n lines from a file and place the remaining content in a new file.

    thanks in advance.

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: delete first n lines from a file
    « Reply #1 on: February 09, 2012, 05:56:28 AM »
    Code: [Select]
    FOR /F "skip=5 tokens=* delims=" %%G in (Myfile.txt) do echo %%G>>NewFile.txt

    jimschel



      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: delete first n lines from a file
      « Reply #2 on: February 17, 2012, 01:33:16 AM »
      If you till tell me the maximum length of the a line, I can make you a quick program that will let you enter the number of lines to skip and then willl copy the rest of the file.


      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: delete first n lines from a file
      « Reply #3 on: February 17, 2012, 06:38:44 AM »
      If you till tell me the maximum length of the a line, I can make you a quick program that will let you enter the number of lines to skip and then willl copy the rest of the file.
      That seems kind of limited if you ask me.
      Granted the cmd processor is limited to 8192 bytes to assign to a variable but what are the odds of the lines being that long.

      Dusty



        Egghead

      • I could if she would, but she won't so I don't.
      • Thanked: 75
      • Experience: Beginner
      • OS: Windows XP
      Re: delete first n lines from a file
      « Reply #4 on: February 19, 2012, 11:52:49 PM »
      A For loop will probably not copy blank lines.   Use the More command with the +n switch.
      One good deed is worth more than a year of good intentions.

      Salmon Trout

      • Guest
      Re: delete first n lines from a file
      « Reply #5 on: February 20, 2012, 12:09:55 AM »
      A For loop will probably not copy blank lines.

      Or ones starting with a space or semicolon, I think. And poison characters will stop the script dead.


      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: delete first n lines from a file
      « Reply #6 on: February 20, 2012, 06:51:14 AM »
      Barring any line starting with a colon this will work.  I can change the code as well if the file has a line that starts with a colon.
      Input
      Code: [Select]
      Line:1
      Line:2
      Line:3
      Line:4
      Line:5
      Line:6 next line is blank

      Line:8 next line has special characters
      #!@#$%^&*()+<>"':!%%!
      Line:10
      Script
      Code: [Select]
      @echo off

      for /f "skip=5 tokens=1* delims=:" %%G in ('type "myfile.txt" ^| findstr /n "^"') do echo.%%H>>myfile_new.txt
      Output
      Code: [Select]
      Line:6 next line is blank

      Line:8 next line has special characters
      #!@#$%^&*()+<>"':!%%!
      Line:10

      Dusty



        Egghead

      • I could if she would, but she won't so I don't.
      • Thanked: 75
      • Experience: Beginner
      • OS: Windows XP
      Re: delete first n lines from a file
      « Reply #7 on: February 20, 2012, 11:11:05 PM »
      Keeping to the KISS principle:

      Code: [Select]
      more +n myfile.txt > newfile.txt
      One good deed is worth more than a year of good intentions.

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: delete first n lines from a file
      « Reply #8 on: February 21, 2012, 05:42:01 AM »
      Keeping to the KISS principle:

      Code: [Select]
      more +n myfile.txt > newfile.txt
      As far as I can tell that will not work if the number of lines in the files exceeds 65,534.  Which most of mine do. I do data processing for a living. On the 65,535 line it outputs -- More (4%) -- to the output file and the batch file pauses.

      Salmon Trout

      • Guest
      Re: delete first n lines from a file
      « Reply #9 on: February 21, 2012, 12:25:29 PM »
      As far as I can tell that will not work if the number of lines in the files exceeds 65,534.  Which most of mine do.

      skipline.vbs

      Code: [Select]
      Const ForReading = 1
      Const ForWriting = 2
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      ReadFileName  = wscript.arguments(0)
      LinesToSkip   = wscript.arguments(1)
      WriteFileName = wscript.arguments(2)

      ' for demo purposes; can delete
      '--------------------------------------
      StartRead = Timer
      '--------------------------------------

      Set ReadFile  = objFSO.OpenTextFile (wscript.arguments(0),  ForReading)
      strText = ReadFile.ReadAll
      Readfile.Close

      ' for demo purposes; can delete
      '--------------------------------------
      EndRead = Timer
      ReadTime = EndRead - StartRead
      StartSplit = Timer
      '--------------------------------------

      ArrayOfLines = Split(strText, vbCrLf)

      ' for demo purposes; can delete
      '--------------------------------------
      Endsplit = Timer
      SplitTime = Endsplit - StartSplit
      StartWrite = Timer
      '--------------------------------------

      Set Writefile = objFSO.CreateTextFile (WriteFileName, ForWriting)
      For j = LinesToSkip To UBound(ArrayOfLines)
      Writefile.writeline ArrayOfLines(j)
      Next
      Writefile.Close

      ' for demo purposes; can delete
      '--------------------------------------
      EndWrite = Timer
      TotalTime = EndWrite - StartRead
      WriteTime = EndWrite - StartWrite
      wscript.echo "Number of Lines in file: " & UBound(ArrayOfLines)
      wscript.echo "Read file in             " & ReadTime  & " sec(s)"
      wscript.echo "Split file in            " & SplitTime & " sec(s)"
      wscript.echo "Wrote output file in     " & WriteTime & " sec(s)"
      wscript.echo "Total time taken         " & TotalTime & " sec(s)"
      '--------------------------------------

      Code: [Select]
      C:\Batch\Test\>type 20lines.txt
      This is line 1
      This is line 2
      This is line 3
      This is line 4
      This is line 5
      This is line 6
      This is line 7
      This is line 8
      This is line 9
      This is line 10
      This is line 11
      This is line 12
      This is line 13
      This is line 14
      This is line 15
      This is line 16
      This is line 17
      This is line 18
      This is line 19
      This is line 20

      C:\Batch\Test\>cscript.exe //nologo skipline.vbs "20lines.txt" 3 "outlist.txt"
      Number of Lines in file: 20
      Read file in             0 sec(s)
      Split file in            0 sec(s)
      Wrote output file in     0.0078125 sec(s)
      Total time taken         0.0078125 sec(s)

      C:\Batch\Test\>type outlist.txt
      This is line 4
      This is line 5
      This is line 6
      This is line 7
      This is line 8
      This is line 9
      This is line 10
      This is line 11
      This is line 12
      This is line 13
      This is line 14
      This is line 15
      This is line 16
      This is line 17
      This is line 18
      This is line 19
      This is line 20


      Note: the load and split times for the small file are too short for the VBScript timer function to measure.

      Now for some real files...

      Clist.csv has nearly 300,000 lines like this and is 36 MB in size

      "CreationTime","Length","FullName"
      "04/06/2011 08:43:43","4226277376","C:\pagefile.sys"
      "02/06/2011 21:08:43","3169705984","C:\hiberfil.sys"
      "05/03/2011 08:14:10","2376366352","C:\Documents and Settings\Mike\X15-65732.iso"
      "05/03/2011 08:14:10","2376366352","C:\Users\Mike\X15-65732.iso"
      "07/07/2011 20:39:09","1818939392","C:\Users\Mike\AppData\Local\Microsoft\Windows Virtual PC\Virtual Machines\Windows XP Mode.vhd"
      "07/07/2011 20:39:09","1818939392","C:\Documents and Settings\Mike\AppData\Local\Microsoft\Windows Virtual PC\Virtual Machines\Windows XP Mode.vhd"

      Code: [Select]
      C:\Batch\Test\>cscript.exe //nologo skipline.vbs "Clist.csv" 3 "outlist.txt"
      Number of Lines in file: 294457
      Read file in             1.953125 sec(s)
      Split file in            0.6328125 sec(s)
      Wrote output file in     3.632813 sec(s)
      Total time taken         6.21875 sec(s)

      Big-Clist.csv has nearly a million lines and is 108 MB in size

      Code: [Select]
      C:\Batch\Test\>cscript.exe //nologo skipline.vbs "Big-Clist.csv" 3 "outlist.txt"
      Number of Lines in file: 883371
      Read file in             5.164063 sec(s)
      Split file in            5.390625 sec(s)
      Wrote output file in     10.95313 sec(s)
      Total time taken         21.50781 sec(s)

      System: AMD Phenom II 3.0  GHz 4 GB RAM drive: 7200 rpm SATA


      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: delete first n lines from a file
      « Reply #10 on: February 21, 2012, 01:41:28 PM »
      Nice Job. Vbscript has always been one of those things I have just refused to learn.

      Salmon Trout

      • Guest
      Re: delete first n lines from a file
      « Reply #11 on: February 21, 2012, 01:58:53 PM »
      In my script above, is this:

      Code: [Select]
      Set ReadFile  = objFSO.OpenTextFile (wscript.arguments(0),  ForReading)
      That will work, but for consistency it should be

      Code: [Select]
      Set ReadFile  = objFSO.OpenTextFile (ReadFileName,  ForReading)
      Quote
      Vbscript has always been one of those things I have just refused to learn.

      It's not all that hard, especially if you can use it to do useful things, and it can do so much more than batch. Of course Powershell is the way to go nowadays...

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: delete first n lines from a file
      « Reply #12 on: February 21, 2012, 02:35:08 PM »
      I started pushing myself to use Powershell 2 years ago and then I went on vacationand when I got back to work I said screw it. But it made me realize that object oriented programming is pretty awesome. I was even making gui's with my Powershell scripts.