Home > Software engineering >  Replacing file texxt with sed
Replacing file texxt with sed

Time:08-10

I'm doing a bash script and i have a txt file and i need edit some text inside it

I need to replace this

<h3> >> 1.10 Ping Desde XXXXXXXXXX01-PRD a 10.xxx.xx.xx ==> [ OK ] </h3>

to this

<h3> >> 1.10 Ping Desde XXXXXXXXXX01-PRD a 10.xxx.xx.xx ==> <span style="color: green">[ OK ] </span></h3>

I've trying with sed but with no success.

I've done this

sed -i 's/==> [ OK ]/==> <span style="color:green">[ OK ]</span>/g' "temp.txt"

with a error response

sed: -e expression #1, char 53: unknown option to `s'

then i tried a solution implemented for replacing urls, instead of "/" using "%"

sed -i 's%==> [ OK ]%==> <span style='color:green'>[ OK ]</span>%g' "temp.txt" 

with no error message, but no file text change too.

If someone knows how to do it, i'll be grateful

CodePudding user response:

You would need to escape the opening square brackets for your code to function

$ sed 's%==> \[ OK ]%==> <span style='color:green'>[ OK ]</span>%g' temp.txt

Or

$ sed -E 's#([^[]*)(\[[^]]*])#\1 <span style="color: green">\2</span>#' temp.txt
<h3> >> 1.10 Ping Desde XXXXXXXXXX01-PRD a 10.xxx.xx.xx ==>  <span style="color: green">[ OK ]</span> </h3>

CodePudding user response:

You need to escape [] in the pattern.

$ echo "<h3> >> 1.10 Ping Desde XXXXXXXXXX01-PRD a 10.xxx.xx.xx ==> [ OK ] </h3>" | sed 's/==> \[ OK \]/==> <span style="color:green">[ OK ]<\/span>/g'
<h3> >> 1.10 Ping Desde XXXXXXXXXX01-PRD a 10.xxx.xx.xx ==> <span style="color:green">[ OK ]</span> </h3>
  • Related