I am trying to replace the below tag with the new content along with the tag line. Is there a way we can change the tag along with its values using sed or any other options we have to do this?

Original html Tag (inside th> tag):

th> file-109 /th>
th> file-209 /th>

Change this html tag to:

th>S.No  Name /th>
th>S.No  Name /th>

I tried the below but this doesn't work. Its throwing error.

sed "s/th>file-1.*/'th>`head -5 file-109 | tail -1`/th>'/g" Diff.html
sed "s/th>file-2.*/'th>`head -5 file-209 | tail -1`/th>'/g" Diff.html
  • Do you really have th> with no <, or is that just something you did to make the text appear in your post? Always post exactly what you have, and use the code formatting tools to format code. With code formatting, you can post special characters such as < and >. See how I edited your post. Mar 8, 2017 at 23:38

3 Answers 3


Sed can use many different characters as separators; you choosed /, but / is also present in the string you want to substitute. Therefore, I'd choose :

sed 's:th>file-[0-9]+ /th>:th>S.No name /th>:g' yourfile

Please note, however, that if you want to process anything more complex than that, you should parse HTML - and not use regex.


Don't really know how robust it needs to be but try sed -e "s/file-[^/ ]*/S.No Name/g" Diff.html and to edit the file directly use sed -ie

perl -0777pe '
   s{ (?<=th>\s) file-\d+ (?=\s/th>) }{
      qx/sed -e "4q;d" "$&"/ =~ /.*/;
' Diff.html

If I understand you correctly, then you want all filenames of the kind File-num enclosed in th> and /th> tags in a file to be replaced by the 4th line of the file just selected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .