Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can SED change 1st & last line? 1

Status
Not open for further replies.

marrow

Technical User
Jul 20, 2001
425
US
Need to read/alter first line & last line and keep all other lines as is. Can I do so without writing to a temp file more than once. I need to keep original file so will need a newfile for changes. Where line1 = needs a small change to it's date and line2 needs it's record count modified.

Any advice appreciated.
 
Hi

Yes. Or no. This will change the first line to string "first" and the last line to "last", but will create a new file.
Code:
sed -i '1s/.*/first/;$s/.*/last/' /path/to/file

Feherke.
 
Thanks Feherke, not sure I made it very clear:

1) FileA contains record1 and 2 fields :
20060215|data
sed will only alter the date field 20060215 in FileA and move to FileB, field2 "data" unchanged

2) all other lines/records to be left as is and copied to FileB

3) FileA - record at end of file :
00|005600
again i would use sed to alter in this case field2 "0056600" in FileA and move to FileB. Field 1 unchanged

So I would like to to create FileB with a new 1st & last record and all other data unchanged, but I only want to use FileA and FileB in the operation, no intermediate temp files to be used.

Many Thanks - Marrow

 
Hi

Ok, and what is wrong ?
Code:
[blue]master #[/blue] cat fileA
20060215|data
bla|bla
bla|bla|bla
00|005600
[blue]master #[/blue] sed '1s/.*|/alteredfield|/;$s/|.*/|alteredfield/' fileA > fileB
[blue]master #[/blue] cat fileB
alteredfield|data
bla|bla
bla|bla|bla
00|alteredfield

Feherke.
 
Apogolies Feherke, I misread your reply! Will test..

Thanks Again - Marrow
 
Hi

No problem. I think I misread your question too. The part with keeping the original file I interpreted as making changes in the original file instead of in a copy.

Feherke.
 
All working - Many Thanks

sed "1s/.*00|20060221/00|20060220/;\$s/.*99|$RECORD/99|$NEWRECORD/" FILEIN>>FILEOUT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top