AIX脚本中的sed不能与特殊字符一起使用 [英] sed in AIX script do not work with special chars

查看:340
本文介绍了AIX脚本中的sed不能与特殊字符一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个生成HTML代码的脚本。
在脚本的最后,我需要通过使表中的单词变为粗体来突出显示某些单词。

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.

因此,我需要在代码中替换每个
WORD

WORD

WORD

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

的实例当然,WORD存储在变量$ CODE中,使操作更加困难。

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

我尝试不幸地使用sed命令,但没有成功。

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:也尝试过awk ...但是使用变量却是...

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

编辑:这是我的确切代码,可以帮助所有人。 code_list.txt包含以下内容的列表:代码描述行。

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

推荐答案

如果


  • 您使用代替'

  • you use " instead of '

$ CODE表达式中没有 @ 编辑:并且没有换行符)(替换 / 通过 @

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

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

解决方案的问题是引号,并且替换表达式中还使用了sed regex分隔符 / ,如< / b>

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

编辑-要从文件中替换多个值

如果您有一个文件code_list.txt,其中所有要替换的字符串都在其中每行执行一次

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

这篇关于AIX脚本中的sed不能与特殊字符一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆