AIX脚本中的sed不能与特殊字符一起使用

编程入门 行业动态 更新时间:2024-10-07 09:28:32
本文介绍了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不能与特殊字符一起使用

本文发布于:2023-11-24 14:13:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能与   脚本   特殊字符   AIX   sed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!