Awk 在文件的每一行中替换不匹配的模式字符串
从以下文件:Awk 在文件的每一行中替换不匹配的模式字符串,awk,sed,Awk,Sed,从以下文件: 1 PARSING IN CURSOR #140499 dep=0 tim=4217919222030 sqlid='ftf4q8xj38z7k' 2 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919224736 3 WAIT #1404991: nam='SQL*Net message' ela= 655 tim=4217919225409 4 PARSING IN CURSOR #140499 d
1 PARSING IN CURSOR #140499 dep=0 tim=4217919222030 sqlid='ftf4q8xj38z7k'
2 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919224736
3 WAIT #1404991: nam='SQL*Net message' ela= 655 tim=4217919225409
4 PARSING IN CURSOR #140499 dep=0 tim=4217919225606 sqlid='9fufagwmu041b'
5 WAIT #1404991: nam='redo log sync' ela= 1 tim=4217919225677
6 WAIT #1404991: nam='redo log sync' ela= 736 tim=4217919226432
7 PARSING IN CURSOR #140499 dep=0 tim=4217919226577 sqlid='bzdm0nbr7c036'
8 WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
9 WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
10 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919228849
11 PARSING IN CURSOR #140499 dep=1 tim=4217919225606 sqlid='9fufagwmu041b'
12 WAIT #1404991: nam='PGA memory operation' ela= 603 tim=4217919229470
13 WAIT #1404991: nam='PGA memory operation' ela= 1 tim=4217919229647
14 WAIT #1404991: nam='PGA memory operation' ela= 521 tim=4217919230185
我想显示所有内容,但对于nam='
和'ela=
之间不匹配的SQL*Net message
行内的字符串,我希望这些字符串被替换为其他DB操作
(并在这些字符串前后保留nam='
和'ela=
):
是否有一个sed或awk命令可以让我轻松地执行此操作?使用以下Perl one命令行:
perl -pe 'next if /nam=.SQL[*]Net message. ela=/; s/(nam=)\S.*\s+(ela=)/${1}'\''Other DB operation'\'' ${2}/' in_file > out_file
Perl one liner使用以下命令行标志:-e
:告诉Perl在线查找代码,而不是在文件中。-p
:一次循环输入一行,默认情况下将其分配给$\uu
。在每次循环迭代后添加print$\uz
(nam=)
和(ela=)
周围的括号将这些模式捕获到变量$1
和$2
是任意字符。*
是任何重复0次或以上的字符。\S
是非空白字符。\s
是一个空白字符
另请参见:请尝试使用GNU awk测试的awk解决方案:
awk '/redo/{sub(/redo log sync/, "Other DB operation")}1' Input_file
解释
awk ' # Start program
/redo/{ # On lines matching the regex `redo`
sub(/redo log sync/, "Other DB operation") # replace `redo log sync` with `Other DB Operation`
}1 # print
' Input_file # Input file goes here
给予
使用sed。对于所有不包含“Net message”(!)且随后不包含“PARSE”的行,将该行拆分为3个部分,并将该行替换为第一部分,然后替换为“Other DB Operation”和第三部分。使用GNU awk匹配第三个参数:
$ awk '
match($0,/(.*nam=\047)([^\047]*)(\047 ela.*)/,a) && (a[2] != "SQL*Net message") {
$0 = a[1] "Other DB operation" a[3]
}
1' file
1 PARSING IN CURSOR #140499 dep=0 tim=4217919222030 sqlid='ftf4q8xj38z7k'
2 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919224736
3 WAIT #1404991: nam='SQL*Net message' ela= 655 tim=4217919225409
4 PARSING IN CURSOR #140499 dep=0 tim=4217919225606 sqlid='9fufagwmu041b'
5 WAIT #1404991: nam='Other DB operation' ela= 1 tim=4217919225677
6 WAIT #1404991: nam='Other DB operation' ela= 736 tim=4217919226432
7 PARSING IN CURSOR #140499 dep=0 tim=4217919226577 sqlid='bzdm0nbr7c036'
8 WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
9 WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
10 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919228849
11 PARSING IN CURSOR #140499 dep=1 tim=4217919225606 sqlid='9fufagwmu041b'
12 WAIT #1404991: nam='Other DB operation' ela= 603 tim=4217919229470
13 WAIT #1404991: nam='Other DB operation' ela= 1 tim=4217919229647
14 WAIT #1404991: nam='Other DB operation' ela= 521 tim=4217919230185
请注意,上面的操作是字符串操作,因此将要查找的字符串中的字符以及替换为的字符视为文本,这样您就不需要转义regexp元字符(例如
或*
)或其中的反向引用(例如&
或\
)。请注意
awk '$0!~/SQL\*Net/{gsub(/nam=.*ela/, "nam='\''Other DB operation'\''",$0)} 1' input_file.txt
1 PARSING IN CURSOR #140499 dep=0 tim=4217919222030 sqlid='ftf4q8xj38z7k'
2 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919224736
3 WAIT #1404991: nam='SQL*Net message' ela= 655 tim=4217919225409
4 PARSING IN CURSOR #140499 dep=0 tim=4217919225606 sqlid='9fufagwmu041b'
5 WAIT #1404991: nam='Other DB operation'= 1 tim=4217919225677
6 WAIT #1404991: nam='Other DB operation'= 736 tim=4217919226432
7 PARSING IN CURSOR #140499 dep=0 tim=4217919226577 sqlid='bzdm0nbr7c036'
8 WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
9 WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
10 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919228849
11 PARSING IN CURSOR #140499 dep=1 tim=4217919225606 sqlid='9fufagwmu041b'
12 WAIT #1404991: nam='Other DB operation'= 603 tim=4217919229470
13 WAIT #1404991: nam='Other DB operation'= 1 tim=4217919229647
14 WAIT #1404991: nam='Other DB operation'= 521 tim=4217919230185
sed -ri '/Net message/!{/PARSING/!s/(^.*'"'"')(.*)('"'"'.*)/\1Other DB operation\3/}' file
$ awk '
match($0,/(.*nam=\047)([^\047]*)(\047 ela.*)/,a) && (a[2] != "SQL*Net message") {
$0 = a[1] "Other DB operation" a[3]
}
1' file
1 PARSING IN CURSOR #140499 dep=0 tim=4217919222030 sqlid='ftf4q8xj38z7k'
2 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919224736
3 WAIT #1404991: nam='SQL*Net message' ela= 655 tim=4217919225409
4 PARSING IN CURSOR #140499 dep=0 tim=4217919225606 sqlid='9fufagwmu041b'
5 WAIT #1404991: nam='Other DB operation' ela= 1 tim=4217919225677
6 WAIT #1404991: nam='Other DB operation' ela= 736 tim=4217919226432
7 PARSING IN CURSOR #140499 dep=0 tim=4217919226577 sqlid='bzdm0nbr7c036'
8 WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
9 WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
10 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919228849
11 PARSING IN CURSOR #140499 dep=1 tim=4217919225606 sqlid='9fufagwmu041b'
12 WAIT #1404991: nam='Other DB operation' ela= 603 tim=4217919229470
13 WAIT #1404991: nam='Other DB operation' ela= 1 tim=4217919229647
14 WAIT #1404991: nam='Other DB operation' ela= 521 tim=4217919230185