Sorry but I am still stuck on this problem.
I would like to insert a paragraph before the first match of "<tr> <td nowrap valign="top"/paragraph"
So I use this code:
sed '0,/<tr> <td nowrap valign="top"/ { s/<tr> <td nowrap valign="top"/paragraph\nsd/g }' /var/www/html/INFOSEC/english/news/test.html
However, the program returns me the whole page of that HTML file and no insertion happens.
Also, I would like to insert some values in the variable in sed code; can I do that?
eg. sed -i 's/old/$new/g' file
To insert before a pattern, you have to make sure your pattern matches something in the file.
The “*” above matches any amount of spaces. Maybe that is what’s causing your command to fail. Of course you need the “-i” switch if you want to edit the file in-place.
For the second question,
sed -i 's/old/$new/g' file, that is almost right except you need to use double-quotes (") instead of single-quotes (') in order for string interpolation to work:sed -i "s/old/$new/g" fileSee http://www.linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/sedfaq4_004.html from How to use sed to replace only the first occurrence in a file? for syntax on replacing only the first match.