How can I search the first line and the last line in a text file?

Blitzcrank picture Blitzcrank · Jan 18, 2013 · Viewed 18.6k times · Source

I need to only search the 1st line and last line in a text file to find a "-" and remove it. How can I do it? I tried select-string, but I don't know to find the 1st and last line and only remove "-" from there.

Here is what the text file looks like:

 % 01-A247M15 G70 
N0001 G30 G17 X-100 Y-100 Z0
N0002 G31 G90 X100 Y100 Z45
N0003 ; --PART NO.:  NC-HON.PHX01.COVER-SHOE.DET-1000.050 
N0004 ; --TOOL:  8.55 X .3937 
N0005 ;  
N0006  % 01-A247M15 G70 

Something like this?

$1 = Get-Content C:\work\test\01.I

$1 | select-object -index 0, ($1.count-1)

Answer

CB. picture CB. · Jan 18, 2013

try:

$txt = get-content c:\myfile.txt
$txt[0] = $txt[0] -replace '-'
$txt[$txt.length - 1 ] = $txt[$txt.length - 1 ] -replace '-'
$txt | set-content c:\myfile.txt