Reputation: 3121
What sed command would allow me to append one string to the end of every line, a different string to the start of the first line, and a different string to the end of the fifth line?
So far I have
sed 's/$/<br>/' $FILE1 >> $FILE1_GETS_APPENDED_TO_THIS_FILE
to append a html break statement to the end of each line in the file1. I need to add a header statement around the first 5 lines though. Therefore, the start of the first line needs to have <h3>
appended to it and the end of the fifth line needs </h3>
appened to it.
Thanks!
Upvotes: 2
Views: 902
Reputation: 30580
I like awk for this kind of stuff:
awk 'NR==1{print "<h3>"};{print $0, "<br>"};NR==5{print "</h3>"}' file.htm
Upvotes: 1
Reputation: 37427
It's that easy:
sed 's/$/<br>/; 1 s/^/<h3>/; 5 s/$/<\/h3>/'
As you use $
to match the end of the line, use ^
to match the start of the line.
Then, for each of your commands (such as s
), you can specify a line number (or range) where it should apply.
Upvotes: 3
Reputation: 107739
Add a number before a sed command to apply it only to that line. Add two numbers separated by a comma to specify a line number range. $
stands for the last line. For more information, see “addresses” in your sed manual.
sed -e '1 s/^/<h3>/' -e '5 s/$/<\/h3>/' -e '6,$ s/$/<br>/'
Upvotes: 2
Reputation: 241758
Have a look at "addresses" in sed
manual. You can specify a line number to which a command should be applied:
sed '5s/$/Appended after fifth line/'
Upvotes: 0