简体   繁体   English

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

[英]Find and replace after n matches and before m matches with AWK

Currently I'm using AWK to find and replace a portion of the first three occurrences of a string. 目前,我正在使用AWK查找和替换字符串的前三个匹配项的一部分。 The string is formatted as such, and there are many of these strings in the file: 字符串是这样格式化的,并且文件中有许多这样的字符串:

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

Using this link , I was able to find a method of using AWK to find and replace the first three instances of the string. 使用此链接 ,我能够找到一种使用AWK查找和替换字符串的前三个实例的方法。 I changed it to what I needed it to do, and a snippet of my script is below: 我将其更改为所需的操作,下面是我的脚本的片段:

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

The goal of the above code is to match on any line containing tempID and replace the number that is assigned to tempID with a value held in a variable named $id . 上面的代码的目标是在包含tempID的任何行上进行匹配,并用保存在名为$id的变量中的值替换分配给tempID的数字。 The find and replace works well, but now I want to replace instances 4-9 with a different number. 查找和替换效果很好,但是现在我想用不同的编号替换实例4-9。 I tried the following method, but it still only replaced the first 5 instances of tempID: 我尝试了以下方法,但它仍然仅替换了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

Is there another way to implement this so that that range of values is replaced? 还有另一种方法可以实现这一点,以便替换该范围的值吗? It doesn't have to be with AWK, it can be with sed or any other Linux utility. 它不必与AWK一起使用,可以与sed或任何其他Linux实用程序一起使用。

Edit: Below is an example of what the lines should look like before and after: 编辑:下面是一个示例,这些示例前后各行的外观:

Before: 之前:

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');

After: 后:

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');

Notice how the lines after the third line are changed, but only the third line that matches the pattern .*tempID.* 请注意第三行之后的行如何更改,但是只有与模式.*tempID.*相匹配的第三行才发生更改.*tempID.*

Making up my own sample input file with numbers by the lines that match your target function call just to highlight that similar but not identical lines are ignored: 用与目标函数调用匹配的行用数字组成我自己的示例输入文件,只是为了突出显示相似但不相同的行被忽略:

$ 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');

and using GNU awk for the 3rd arg to match(): 并使用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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM