如何在AWK中替代正则表达式?

时间:2018-08-31 14:29:44

标签: awk

用AWK脚本替换正则表达式的最简单方法是什么?
我的目标是搜索模式:

/a|b/

在句子中:

I read a book "GAWK: Effective AWK Programming", but I can not figure it out.

并替换为:

ABC/a|b/ABC

结果将是:

I reABCaABCd ABCaABC ABCbABCook "GAWK: Effective AWK ProgrABCaABCmming", but I cABCaABCn not figure it out.

2 个答案:

答案 0 :(得分:2)

您可以尝试:

$ awk '{ gsub(/[ab]/,"ABC&ABC")}1' file 
I reABCaABCd ABCaABC ABCbABCook "GAWK: Effective AWK ProgrABCaABCmming", ABCbABCut I cABCaABCn not figure it out.

文件在哪里:

I read a book "GAWK: Effective AWK Programming", but I can not figure it out.

答案 1 :(得分:1)

sed对于此任务可能更好

echo "I re..." | sed 's/[ab]/ABC&ABC/g'