0

I am writing a bash script using awk with variable passing in AIX powerpc machine. 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. My requirement (see my script below): - if awk pattern matching is true, print $0 - else print "transaction: p value not found."

Content of trans.txt

bash-4.2$ cat trans.txt

10291413
8537353
8619033
8619065
8625705

Could someone help me please. Thank you.

content of 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
5
  • {print $0} could become {print $0;next} {do other thing} Commented Oct 22, 2014 at 22:59
  • Thanks for the speedy comment, Mark. I tried that it did not work as expected. Commented Oct 22, 2014 at 23:11
  • {print $0;next} {print "transaction: p value not found."} Commented Oct 22, 2014 at 23:19
  • I updated my script as above. The output: "transaction: p value not found" was repeated for 4000 lines I guess that's number of $sysDir/$syslogFile line of record. I noticed the awk syntax is quite temperamental in bash / AIX flavor. That's why I cant make the simple if / else statement to work. Commented Oct 22, 2014 at 23:27
  • What is in trans.txt and where is $filename set? Commented Oct 23, 2014 at 7:19

1 Answer 1

0
awk -v p="$concatPattern" '
    $0 ~ p {print $0; found=1}
    END   {if (! found) print "transaction: p value not found"}
' $sysDir/$syslogFile
Sign up to request clarification or add additional context in comments.

8 Comments

I think we got there in the end :-)
The answer does not meet my expectation. I did some troubleshooting with my above code and make a conclusion (I might be wrong on this, please confirm). The awk find match statement I used above will only execute a true boolean value, therefore ; found=1 never get executed for unmatch eg. false boolean. The END statement becomes useless. I have tested it myself. What I meant was awk cannot tell me when the pattern does not match, it simply do nothing.
@ChristinaLiu maybe you can change slightly one of your requirements, so that Glenn can write print "transaction:",p,"not found", i.e., a more informative warning.
Do you need awk to tell you which lines did not match the pattern, or which files did not contain the pattern?
No, I didn't change the original requirement. The solution provided above can not spit out "transaction: p not found" for unmatch pattern.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.