简体   繁体   English

bash脚本awk,带有传递变量和if语句

[英]bash script awk with passing variable and if statement

I am writing a bash script using awk with variable passing in AIX powerpc machine. 我正在使用awk编写bash脚本,并在AIX powerpc机器中传递变量。 The code written works fine after I read some questions & answers in this site. 在阅读本网站的一些问题和答案后,编写的代码可以正常工作。 :) :)

Now, I'd like to add if statement in my bash script and I got awk syntax errors. 现在,我想在bash脚本中添加if语句,但出现awk语法错误。 My requirement (see my script below): - if awk pattern matching is true, print $0 - else print "transaction: p value not found." 我的要求(请参阅下面的脚本):-如果awk模式匹配为true,则打印$ 0-否则打印“事务:找不到p值”。

Content of trans.txt trans.txt的内容

bash-4.2$ cat trans.txt

10291413
8537353
8619033
8619065
8625705

Could someone help me please. 有人可以帮我吗。 Thank you. 谢谢。

content of getDetail.sh getDetail.sh的内容

#!/usr/bin/bash

sysDir="/var/syslog-ng/TEST/STS"
syslogFile="DPSTSLog2014-10-22T09.log"
pattern="Latency"

filename=$1
while read f; do
  concatPattern="$f.*$pattern"
  awk -v p="$concatPattern" '$0 ~ p {print $0}' $sysDir/$syslogFile
done < $filename

run command execution below 在下面运行命令执行

./getDetails.sh trans.txt

result 结果

2014-10-22T09:15:53+11:00,10.16.198.50,info,latency,[info] xmlfirewall(ws-TrustValidate): trans(10291413): Latency:   0   0   0   0  14  14   0  14   0   0   0  14   0   0   0   0
2014-10-22T09:15:38+11:00,10.16.198.50,info,latency,[info] xmlfirewall(ws-TrustValidate): trans(8619033): Latency:   0   0   0   0  73  73   0  73   0   0   0  73   0   0   0   0
2014-10-22T09:18:04+11:00,10.16.198.50,info,latency,[info] xmlfirewall(ws-TrustValidate): trans(8625705): Latency:   0   0   0   0  13  13   0  13   0   0   0  13   0   0   0   0
awk -v p="$concatPattern" '
    $0 ~ p {print $0; found=1}
    END   {if (! found) print "transaction: p value not found"}
' $sysDir/$syslogFile

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

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