0

I created a script that is generating HTML code. At the end of the script, I need to hightlight some words in a table by making them bold.

So I need to replace in the code, every instance of WORD by WORD or WORD

of course, WORD is stored in a variable $CODE to make this much more harder.

I tryed to used the sed command unfortunatelly with no success.

 sed -e 's/$CODE/<b>$CODE</b>/g' page.html > newpage.html

I tryed also with double quotes, with escape chars before the /, many combination of quotes and double quotes, with | as delimiter, always with no success. If any one of you could point me to a solution, I would be really greatfull.

PS : Tryed also with awk ... but with variable it's a real pain in the ...

EDIT : Here is my exact code to help all of you. code_list.txt contain a list of CODE-description lines.

 for y in code_list.txt
 do
      CODE=$(echo $y | cut -f1 -d-)
      awk -v code="$CODE" '{if(match($0,code)){gsub(code,"<b>"code"</b>")}} 1' $SCRIPTS/html/daily_running_jobs.html > $SCRIPTS/html/daily_running_jobs_highlight.html
      mv $SCRIPTS/html/daily_running_jobs_highlight.html $SCRIPTS/html/daily_running_jobs.html  
 done

  • small precision, if I echo the command, variables are well translated. But command not executed. – Boris Bielecki Jun 04 '16 at 15:51
  • 2
    [edit] your question to include concise, testable sample input and expected output. Include those use cases you think it'll be hard to handle, and if your "WORD"s can include any non-alphabetic characters then describe+show those cases too. Also include cases like 'the' and 'There` so we can see how to handle partial and case-sensitive matching. Finally - don't use all-upper-case for non-exported variable names to avoid clashes with builtin and other exported variable names. – Ed Morton Jun 04 '16 at 20:29

2 Answers2

2

There is no reason your idea would not work if

  • you use " instead of '

  • there is no @ (EDIT: and no linebreak) in the $CODE expression (replace / by @)

sed -e "s@$CODE@<b>$CODE</b>@g" page.html > newpage.html

The problem with your solution was the quoting, and that the sed regex separator / was also used in your replacement expression as in </b>

EDIT - multiple values to be replaced from file

If you have a file code_list.txt where all strings-to-be-replaced are in one-per-line, do

cp page.html  newpage.html
while read var
do
    mv newpage.html newpage1.html
    sed -e "s@$var@<b>$var</b>@g" newpage1.html > newpage.html
    # optionally rm newpage1.html
done < code_list.txt
Stefan Hegny
  • 1,942
  • 4
  • 19
  • 23
  • I will give it another try, but I think I already tryed this with | instead of @. I'll tell you more tomorrow. – Boris Bielecki Jun 05 '16 at 08:02
  • Sorry Stefan, but it's not working. I have that error message. Variable CODE was replaced by NTCA : sed: Function s@NTCA cannot be parsed. – Boris Bielecki Jun 06 '16 at 07:00
  • From you comment to Chet's answer I suppose that your variable CODE does not contain "NTCA" but "NTCA EBXL FSRV" and possibly even a NEWLINE char somewhere in there - can you please verify if this is intended or if the code is supposed to work if only "NTBA" with nothing after is the value of $CODE - please report the output of `echo ">>$CODE< – Stefan Hegny Jun 06 '16 at 07:23
  • My replace command is located in a << for CODE in code_list.txt >> loop where code_list.txt contain one code per line. So I assume that it should use the first line code, then do the awk sed command, then the second code and etc. – Boris Bielecki Jun 06 '16 at 07:49
  • how come you are "assuming" it? I think it won't do by itself, you have to code a loop around - see my edit - if would have been easier if you had specified this in the original question – Stefan Hegny Jun 06 '16 at 07:51
  • Sorry , should have read your complete comment before going on - but `for CODE in code_list.txt` would only process the value `code_list.txt` – Stefan Hegny Jun 06 '16 at 07:59
  • 1
    Sorry again Stefan but it is still not working. I echoed the command to check, and the var is well replaced by the codes, but the sed command is not executed. I had to remove the -i as it is not supported on my system. I used a > output.html to write the sed output in another file. With no more success. – Boris Bielecki Jun 06 '16 at 08:11
  • ok, if -i is not working we have a problem because each run will overwrite the previous output file and you will end up only with the last replacement being present in the output.html -I'll edit again – Stefan Hegny Jun 06 '16 at 08:12
  • Thank you Stefan, how could I have missed that I'm always updating the same file. I added the file swaping, but unfortunately, the sed command is still not executed :-( – Boris Bielecki Jun 06 '16 at 08:37
  • 1
    My bad Stefan. It's working like a charm ! I was looking the wrong html file :-) Thank you very much for your support. – Boris Bielecki Jun 06 '16 at 08:52
0
awk -vcode="$CODE" '{if(match($0,code)){gsub(code,"<b>"code"</b>")}} 1' page.html > newpage.html

assume there is a variable $CODE

We pass it to awk variable, if there is a match then replace

Chet
  • 1,175
  • 1
  • 11
  • 20
  • Thank you Chet. I'll try this on monday @work. – Boris Bielecki Jun 04 '16 at 18:05
  • 1
    awk is not C: `'match($0,code){gsub(code,""code"")} 1'` but that will fail given various values of `code`, just like an equivalent sed script would, so idk if it's the right answer. Also, by not putting a space between `-v` and `code` you make the script unnecessarily gawk-specific, just put a space in so it's portable: `-v code=...` – Ed Morton Jun 04 '16 at 20:26
  • Hi, I just tryed the awk option, but it's not working better : #highlight the important application codes for y in $(cat $SCRIPTS/$APP) do CODE=$(echo $y | cut -f1 -d-) awk -v code="$CODE" '{if(match($0,code)){gsub(code,""code"")}} 1' $SCRIPTS/html/daily_running_jobs.html > $SCRIPTS/html/daily_running_jobs_highlight.html done – Boris Bielecki Jun 06 '16 at 07:13
  • It give me that error : + awk -v code=NTCA EBXL FSRV {if(match($0,code)){gsub(code,""code"")}} 1 /home/ppcm980x/ctm_em/etc/emweb/tomcat/webapps/commvault/scripts/html/daily\ _running_jobs.html + 1> /home/ppcm980x/ctm_em/etc/emweb/tomcat/webapps/commvault/scripts/html/daily_running_jobs_highlight.html awk: String NTCA cannot contain a newline character. The source line is 1. The error context is >>> <<< awk: String NTCAEBXL cannot contain a newline character. The source line is 1. awk: String NTCAEBXLEG cannot contain a newline character. The source line is 1. – Boris Bielecki Jun 06 '16 at 07:15
  • I have to precide that if I hard code a value in -v code="TEST", then it's working. But using a variable here do not work. – Boris Bielecki Jun 06 '16 at 07:36