tags:

views:

39

answers:

1

In Powershell - Say I want to delete line 5 from a 10 line text file using the line number, how is this done?

Thanks, Daniel

+2  A: 

Not as clean as I would like but does the trick:

$content = Get-Content foo.txt
$content | Foreach {$n=1}{if ($n++ -ne 5) {$_}} > foo.txt

If you have PSCX installed, you can use the Skip-Object cmdlet to make this a little nicer:

$content = Get-Content foo.txt
$content | Skip -Index 4 > foo.txt

Note that the -Index parameter is 0 based which is why I used 4 instead of 5.

Keith Hill