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

问题描述 投票:-1回答:2

用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.
awk
2个回答
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
投票

sed可能更适合这项任务

echo "I re..." | sed 's/[ab]/ABC&ABC/g'
© www.soinside.com 2019 - 2024. All rights reserved.