Dominus
Dominus

Reputation: 35

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

<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.

Upvotes: 1

Views: 48

Answers (1)

sseLtaH
sseLtaH

Reputation: 11237

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

Upvotes: 2

Related Questions