简体   繁体   English

使用awk在BASH中的特定列中搜索变量

[英]Search A Variable in Specific Column in BASH with awk

I have LOG_FILE like below:我有 LOG_FILE 如下所示:

LOG|Server301|2015|05|12|00|58|35|572617|0|COMP|002.01.003.00|DSOHMSN2ViewCallback.cpp|142|CALLBACK:DSOHMSN2ViewCallback::FLIPBypassOn Changed SN=2 old value =2 new value =1
LOG|Client2|2015|05|12|00|58|35|593088|0|COMP|002.01.003.00|AMSN.cpp|12577|CALLBACK:OHMOAM::ohmSN2ViewChange, Old FLIPBypassOnMT
LOG|Server302|2015|05|12|00|58|35|593116|0|COMP|002.01.003.00|AMSN.cpp|12590|CALLBACK:OHMOAM::ohmSN4ViewChange, New FLIPBypassOnMT
LOG|Server301|2015|05|12|00|58|35|593302|0|COMP|002.01.003.00|AMSN.cpp|12577|CALLBACK:OHMOAM::ohmSN3ViewChange, Old disableFLIPBypassOnMT
LOG|Client1|2015|05|12|00|58|35|593324|0|COMP|002.01.003.00|AMSN.cpp|12590|CALLBACK:OHMOAM::ohmSN2ViewChange, New disableFLIPBypassOnMT

I just want to print line no which have我只想打印没有的行

  • word Server at 2nd column第 2 列的 word Server

  • word CALLBACK at 15th column第 15 列的字CALLBACK

  • variable $parameter_name at 15th column第 15 列的变量$parameter_name

I executed the below command:我执行了以下命令:

parameter_name="FLIPBypassOnMT"
$(awk -v var="$parameter_name" -F'|' '$2 ~ /Server/ && $15 ~ /CALLBACK/  && index($15,var) { print NR}' $LOG_FILE 2>/dev/null | tail -1)

But Not getting the line no.但没有得到线号。 I am using sun4-solaris.我正在使用 sun4-solaris。 please help me out.请帮帮我。 NB:注意:

$(awk -v var="$parameter_name" -F'|' '$2 ~ /Server/ && $15 ~ /CALLBACK/ { print NR}' $LOG_FILE 2>/dev/null | tail -1) 

is running fine.运行良好。 but need all the three criteria.但需要所有三个标准。

Have you tried你有没有尝试过

$(awk -v var="$parameter_name" -F'|' '$2 ~ /Server/ && $15 ~ /CALLBACK/  && $15 ~ var { print NR}' $LOG_FILE 2>/dev/null | tail -1)

I'm not sure why you used the index function when you can use the ~ regex operator again.当您可以再次使用~ regex 运算符时,我不确定您为什么使用 index 函数。

I would suspect that the problem lies elsewhere.我怀疑问题出在其他地方。

If如果

awk blah blah

prints 23 then然后打印23

$(awk blah blah)

will attempt to execute 23 as a shell command (and usually fail).将尝试将23作为 shell 命令执行(通常会失败)。 Remove the process substitution at least while debugging this problem.至少在调试此问题时删除进程替换。

Tangentially, some Sun systems have a very limited Awk, but I guess that's not the problem here.顺便说一下,一些 Sun 系统的 Awk 非常有限,但我想这不是问题所在。 Still, you might want to google for "Solaris XPG4 Awk".不过,您可能想在 google 上搜索“Solaris XPG4 Awk”。

谢谢你们@tripleee 和@bkmoney 我已经使用了下面的命令并且工作正常:

$(nawk -v var="$parameter_name" -F'|' '$2 ~ /Server/ && $15 ~ /CALLBACK/  && $15 ~ var { print NR}' $LOG_FILE 2>/dev/null | tail -1)

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

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