0

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
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Leo Chan
  • 4,217
  • 8
  • 31
  • 47

1 Answers1

4

To insert before a pattern, you have to make sure your pattern matches something in the file.

jcomeau@intrepid:/usr/src/clusterFix$ cat test.html
<tr> <td nowrap valign="top">blather blather blather</td></tr>
jcomeau@intrepid:/usr/src/clusterFix$ sed  '/<tr> *<td nowrap valign="top"/i<p>this is a new paragraph</p>' test.html
<p>this is a new paragraph</p>
<tr> <td nowrap valign="top">blather blather blather</td></tr>

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" file

See 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.

jcomeau@intrepid:/tmp$ cat test.txt 
this is not the test
this is not the test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
jcomeau@intrepid:/tmp$ sed '0,/\(this is a test\)/s//before first match\n\1/' test.txt
this is not the test
this is not the test
before first match
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
Community
  • 1
  • 1
jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
  • Thanks can i use this add some xml tag by using sed just like this??? sed '0,// { //\naaa<\/title>\n<guid>ccc<\/guid>\n<link/>ddd<\/link>\n<description><![CDATA[<p>dsfsd<\/p>]]><\/description>\n<\/item>\n }' /var/www/html/INFOSEC/english/rss/test.xml</p></description></guid> – Leo Chan Jul 19 '11 at 05:02
  • probably not. do you know what the '0,/pattern/' does? it matches everything between the start of the file and the pattern. and what are you doing with the curly braces "{}"? – jcomeau_ictx Jul 19 '11 at 05:07
  • so how can i modify html/xml tag with sed??? all < > change to lt; something???thanks your help – Leo Chan Jul 19 '11 at 05:10
  • you can use it, just learn the syntax. you seem to be making it more complicated than it really is. – jcomeau_ictx Jul 19 '11 at 05:13
  • the braces there is that i want to insert before ONLY THE FIRST match. sed -i '0,/** – Leo Chan Jul 19 '11 at 05:15
  • http://www.linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/sedfaq4_004.html from http://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file – jcomeau_ictx Jul 19 '11 at 05:25
  • yup the reference is helpful, how about insert before the first match? thx – Leo Chan Jul 19 '11 at 05:31
  • see edited answer. I've never used this syntax before, I'm not familiar with it. – jcomeau_ictx Jul 19 '11 at 05:37
  • yeah it somehow works but it replace my orginal text (eg this is a test) – Leo Chan Jul 19 '11 at 06:48
  • oops, my bad. fixed, see revised answer. – jcomeau_ictx Jul 19 '11 at 07:00
  • thx i use another stupid way which replace the old string with some new thing plus old string – Leo Chan Jul 19 '11 at 07:12