header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
How to remove certain lines in text files?
Last Post 04 Aug 2010 10:46 AM by absolutebeginner. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
absolutebeginnerUser is Offline
New Member
New Member
Posts:14
Avatar

--
02 Aug 2010 12:17 PM
    Hello, I have around 35000 text files (ASCII) from which I need to delete the lines 2, 3, 4 and 5. How could I do this with a PS script? Thank you very much!
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    02 Aug 2010 12:34 PM
    Try this (make sure you test it on just one file first :) ):

    $path = "file.txt"
    $lines = Get-Content -Path $path
    1..4 | ForEach-Object { $lines[$_] = $null }
    Set-Content -Path $path -Value $lines
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    02 Aug 2010 12:51 PM
    Try this on a test file:

    $file = get-content test.txt
    $file[0],$file[5..($file.length-1)] | out-file test.txt -encoding ascii

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    02 Aug 2010 12:57 PM
    want another?

    (gc file.txt) | ? {(2..5) -notcontains $_.ReadCount} | sc file.txt
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    absolutebeginnerUser is Offline
    New Member
    New Member
    Posts:14
    Avatar

    --
    04 Aug 2010 10:46 AM
    Thanks! I used Shays code. Worked perfectly.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer