header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Delete first 10 lines of a file
Last Post 26 Jun 2008 11:00 AM by BauerShell. 9 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
duhaasUser is Offline
New Member
New Member
Posts:4
Avatar

--
20 Jun 2008 09:56 PM  

trying to find a script that will delete first 10 lines from a file.  thanks in advance

marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:195
Avatar

--
21 Jun 2008 04:01 PM  

There's nothing that can actually delete lines from an existing file, that I'm aware of.

You'll have to do something like this:

get-content test.txt|select-object -last ((get-content test.txt).count - 9)|out-file test2.txt

What you're doing is creating a new file.  You could then overwrite the original with the new.

Marco

 

 

Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
duhaasUser is Offline
New Member
New Member
Posts:4
Avatar

--
21 Jun 2008 04:31 PM  
Thanks for the help, in doing this:

gc F:\MonthlyINV053108.csv | Select-Object -first ((gc F:\MonthlyINV053108.csv).count -11)|out-file test.txt

it doesnt appear to be removing the first 11 lines, but it is removing the last 11??
KarlUser is Offline
New Member
New Member
Posts:11
Avatar

--
21 Jun 2008 07:54 PM  
Here is something that works.. and is also pipeline friendly, and more preformant because you arne't reading the same file in twice.

get-Content c:\test.csv | foreach -begin {$count = 0 } -process{$count++;if ($count -gt 10 ) {$_} } | out-File c:\test2.csv

however this ability to skip so many items of anything in a pipeline is useful in general so we can distill this logic down to a function that can help us.

function skip([int]$amount) {
begin {$count = 0 }
process {$count++;if ($count -gt $amount ) {$_} }
}

and then with this function our one liner is much cleaner

get-Content c:\test.csv | skip 10 | out-File c:\test2.csv

jsnoverUser is Offline
New Member
New Member
Posts:1
Avatar

--
21 Jun 2008 10:14 PM  
In PS V2, we have Select -SKIP
cat test.csv |select -skip 10

jps
duhaasUser is Offline
New Member
New Member
Posts:4
Avatar

--
23 Jun 2008 05:26 PM  

Thanks everyone for the wonderful advice.  Being new to powershell its really nice to have a community like this to bounce ideas off of

duhaasUser is Offline
New Member
New Member
Posts:4
Avatar

--
23 Jun 2008 05:27 PM  
I do still have one question, and its not a huge deal, but the original .txt file I bring in is 9MB in size, and the output that it spits out is 17.5MB in size, any reason for the increase in size if I'm just removing the first 7 lines.
marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:195
Avatar

--
23 Jun 2008 05:31 PM  
Posted By duhaas on 06/21/2008 8:31 AM
Thanks for the help, in doing this:

gc F:\MonthlyINV053108.csv | Select-Object -first ((gc F:\MonthlyINV053108.csv).count -11)|out-file test.txt

it doesnt appear to be removing the first 11 lines, but it is removing the last 11??



Note: You used "-first", and I had used "-last".

Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
KarlUser is Offline
New Member
New Member
Posts:11
Avatar

--
23 Jun 2008 05:35 PM  

ahh the default format for out-file is UNICODE, which will explain the filesize increase

you can force ascii with

 

out-file -encoding ASCII

 

/Karl

BauerShellUser is Offline
New Member
New Member
Posts:7
Avatar

--
26 Jun 2008 11:00 AM  
another smooth way would be to directly index into the content

(gc "F:\MonthlyINV053108.csv ")[10..((gc "F:\MonthlyINV053108.csv ").length)] | Out-File -encoding ASCII test.txt
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer