在AWK的n个匹配项之后和m个匹配项之前查找并替换

时间:2018-12-03 22:21:18

标签: linux bash awk sed scripting

当前,我正在使用AWK查找和替换字符串的前三个出现部分。字符串是这样格式化的,并且文件中有许多这样的字符串:

func(tempID="39849235",count='12');

使用this link,我找到了一种使用AWK查找和替换字符串的前三个实例的方法。我将其更改为所需的操作,下面是我的脚本的片段:

id=12349876
awk 'BEGIN {matches=0}
     matches < 3 && /.*tempID.*/ { sub(/tempID=.[0-9]+./,"tempID='"$id"'"); matches++ }
     { print $0 }' filName.py >filName.py.changed

以上代码的目标是在包含tempID的任何行上进行匹配,并用变量$id中保存的值替换分配给tempID的数字。查找和替换效果很好,但是现在我想用不同的编号替换实例4-9。我尝试了以下方法,但它仍然仅替换了tempID的前5个实例:

id2=39843237
awk 'BEGIN {matches=4}
     matches < 9 && /.*tempID.*/ { sub(/tempID=.[0-9]+./,"tempID='"$id2"'"); matches++ }
     { print $0 }' filName.py >filName.py.changed

还有另一种方法可以实现这一点,以便替换该范围的值吗?它不必与AWK一起使用,可以与sed或任何其他Linux实用程序一起使用。

编辑:以下是各行前后的样例:

之前:

func2(blah)
func3(blah)
func(tempID="83747432",count='12');
func(tempID="83747432",count='12');
func(tempID="83747432",count='12');
func(tempID="83747432",count='12');

func(tempID="83747432",count='12');
func(tempID="83747432",count='12');

之后:

func2(blah)
func3(blah)
func(tempID="83747432",count='12');
func(tempID="83747432",count='12');
func(tempID="83747432",count='12');
func(tempID="39843237",count='12');

func(tempID="39843237",count='12');
func(tempID="39843237",count='12');

请注意第三行之后的行如何更改,但仅更改与模式.*tempID.*匹配的第三行

1 个答案:

答案 0 :(得分:1)

用与目标函数调用相匹配的行用数字组成我自己的示例输入文件,只是为了突出显示相似但不相同的行被忽略:

$ cat file
1 func(tempID="39849235",count='12');
boofunc(tempID="39849235",count='12');
2 here is one func(tempID="39849235",count='12'); right there
func(tempID="99999999",count='12');
func(tempID="39849235",count='123');
3 and another:           func(tempID="39849235",count='12');
4 func(tempID="39849235",count='12');
5 func(tempID="39849235",count='12');
6 func(tempID="39849235",count='12');
boofunc(tempID="39849235",count='12');
7 here is one func(tempID="39849235",count='12'); right there
func(tempID="99999999",count='12');
func(tempID="39849235",count='123');
8 and another:           func(tempID="39849235",count='12');
9 func(tempID="39849235",count='12');
10 func(tempID="39849235",count='12');

,并使用GNU awk将第三个参数匹配():

$ cat tst.awk
match($0,/(.*\<func\(tempID=")39849235(",count='12'\);.*)/,a) {
    ++cnt
    if ( (cnt >= beg) && (cnt <= end) ) {
        $0 = a[1] id a[2]
    }
}
{ print }

$ id=12349876
$ awk -v id="$id" -v beg=1 -v end=3 -f tst.awk file
1 func(tempID="12349876",count='12');
boofunc(tempID="39849235",count='12');
2 here is one func(tempID="12349876",count='12'); right there
func(tempID="99999999",count='12');
func(tempID="39849235",count='123');
3 and another:           func(tempID="12349876",count='12');
4 func(tempID="39849235",count='12');
5 func(tempID="39849235",count='12');
6 func(tempID="39849235",count='12');
boofunc(tempID="39849235",count='12');
7 here is one func(tempID="39849235",count='12'); right there
func(tempID="99999999",count='12');
func(tempID="39849235",count='123');
8 and another:           func(tempID="39849235",count='12');
9 func(tempID="39849235",count='12');
10 func(tempID="39849235",count='12');

$ id=12349876
$ awk -v id="$id" -v beg=4 -v end=9 -f tst.awk file
1 func(tempID="39849235",count='12');
boofunc(tempID="39849235",count='12');
2 here is one func(tempID="39849235",count='12'); right there
func(tempID="99999999",count='12');
func(tempID="39849235",count='123');
3 and another:           func(tempID="39849235",count='12');
4 func(tempID="12349876",count='12');
5 func(tempID="12349876",count='12');
6 func(tempID="12349876",count='12');
boofunc(tempID="39849235",count='12');
7 here is one func(tempID="12349876",count='12'); right there
func(tempID="99999999",count='12');
func(tempID="39849235",count='123');
8 and another:           func(tempID="12349876",count='12');
9 func(tempID="12349876",count='12');
10 func(tempID="39849235",count='12');
相关问题