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!

Using sed to replace a line

Status
Not open for further replies.

alowance

IS-IT--Management
Feb 20, 2004
1
CH
I need to add an entry into a file, right before the last line. The last line is the same on most of thefile, so I thoutht I coul serach it out and eith insert my new line or deleter this line and replace it with two line (my new line and the original line I deleted))I have tried the -i command but am not having luck. Any help would be appreciated. Thanks.
 
Try something like this:
echo "\$i\nYourTextHere\n.\nw\nq" | ed -s yourfile.txt

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
With sed :
[tt]
sed '$iLine to be inserted' file.txt > tmp.txt
mv tmp.txt file.txt
[/tt]
The disadvantage of 'sed' is that should be used an intermediate file

Jean Pierre.
 
Or with awk

NR>1{print a}
{a=$0}
END {
print "your text"
print a
}


CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top