I was trying to insert a values in between html tags in a specific line number. Also it should ignore whitespaces at the beginning. Example :- line 15 should be replaced


this should be replaced with:

  • Can you give input examples and desired outputs? – Egor Vasilyev Nov 20 '17 at 9:52
  • edited my question – John Nov 20 '17 at 9:53
  • sed is bad tool for parsing html documents. Post the extended html markup to show the adjacent context lines – RomanPerekhrest Nov 20 '17 at 10:16

You could use simple s/// pattern:

echo "<john></john>" | sed 's/<john><\/john>/<john>hello<\/john>/'

Output will be:


To replace certain line in file use this command:

sed '15s/<john><\/john>/<john>hello<\/john>/' file

sed will replace only first occurance in 15 line

To simplify your command you could use | as delimeter instead of default /:

sed '15s|<john></john>|<john>hello</john>|' file
| improve this answer | |
  • If it as simple as written by the OP then sed '15s/></>hello</' – bu5hman Nov 20 '17 at 11:24
  • @bu5hman, yes, you are right. But what if this line contains other tags?.. – Egor Vasilyev Nov 20 '17 at 13:19
  • The OP question pertains to a single set of tags on a known line so it works and I could equally ask you ".....what if the tags aren't John...."? We are both correct but the key point to take away is that the OP must identify the unique delimiters in any given case. – bu5hman Nov 20 '17 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.