1

I want to insert a string in html before the first "<p" in the file, however I am having some difficulty.

thestring="<p>this is some sentence</p>"
sed -i "/<p/i${thestring}" somefile.html

"somefile.html" however contains multiple lines containing "<p" and the above inserts the string multiple times.

I have tried doing this:

sed -i "0,/<p/i${thestring}" somefile.html

or

sed -i "0,/<p/s//i${thestring}" somefile.html

but I'm getting no where.

What am I doing wrong?

Stephen
  • 149
  • 3
  • 12
  • this link may help: http://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file – user1502952 Nov 20 '13 at 11:11

2 Answers2

2

two things you may consider :

1) use s/.../.../ instead of i

2) special chars in your thestring which would conflict with sed's s separator. In your example the slash in/p>

this should work for your needs:

 sed  -i "0,/<p/ s_^_$thestring\n&_" file
Kent
  • 189,393
  • 32
  • 233
  • 301
0
echo "${thestring}" | sed s'@<p>this is@blah blah <p>this is@'

If you want to get a specific instance of "

", just add a bit more context for the search.

petrus4
  • 616
  • 4
  • 7