Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sed wildcard and back reference

I have a text log file that I need to add some basic html like stuff (to match decades of older logs).

It basically looks like this in all lines

<constant> <various> text

and should transform to this in each line

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<font color="#7a40b0">constant:</font><font color="#0000fc">various:</font> text

constant always remains the same, various changes (these are usernames, could be anything, e.g. F0oBar 1)

I tried

sed 's|'\<constant\>\ \<'.*'\>'|'\<font\ color\=\"#7a40b0\"\>constant:\</font\>\<font\ color\=\"#0000fc\"\>\1:\</font\>'|g'

but this just returns

<font color="#7a40b0">constant:</font><font color="#0000fc">1:</font>

I tried searching on how to make the back reference work but I’m sure now I’m messing up the wildcard. As soon as I use parenthesis, brackets and/or ^ stuff breaks (the regex doesn’t match anymore).
I’ve used https://sed.js.org to give me a visual help so I hope this gave me correct feedback.

>Solution :

Using sed

$ sed -E 's|<([^ ]*)> <([^ ]*)>|<font color="#7a40b0">\1:</font><font color="#0000fc">\2:</font>|' input_file
<font color="#7a40b0">constant:</font><font color="#0000fc">various:</font> text
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading