Bash – How to populate end tags using sed, awk or any other command

awkbashsedtext processing

I have a txt file that looks like this:

<sss>ss<

or

<firstword>anotherword<

I want it to look like this

<sss>ss</sss>

or

<firstword>anotherword</firstword>

Basically taking the first word and placing it in an end tag.

And before you asked what I have tried, the answer is nothing, I couldn't think of anything.

Best Answer

With input:

<abc>def<
<firstword>anotherword</firstword>
<ghi>klm<

Use:

sed 's/<\([^>]*\)>\(.*\)<$/<\1>\2<\/\1>/' input

Output:

<abc>def</abc>
<firstword>anotherword</firstword>
<ghi>klm</ghi>

The sed line only affects lines ending in < (because of the <$) and catches the patterns between the first <> pair and between '><' and pastes everything back in duplicating the first pair at the end (plus a closing '>')

Related Question