1

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.

HatLess
  • 10,622
  • 5
  • 14
  • 32
Dominus
  • 35
  • 8
  • Please [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Nov 13 '22 at 23:23
  • Not everything with <> is XML/HTML, it's a log file that just has user names in , no xml structure whatsover – Dominus Nov 14 '22 at 00:00

1 Answers1

2

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
HatLess
  • 10,622
  • 5
  • 14
  • 32