I have a project I am working on for work. The script I am building initially takes two arguments: one is an unique Identifier and the other is for a log file (both strings). Now I have the first part of it done which is parsing for the unique Identifier. But the second part, is parsing for a specific piece of string which is a randomly generated string of numbers and letters.

I am using a awk statement to get this random string and I have verified that my expression is correct in Rubular. But when I attempt to test the code with more, nothing prints out. Here is what I typed in the command line:

awk '/\s=\s[a-zA-Z0-9-]+/ {print }' ~/jlog/server.log | more

Here is what I have in my script:

for j in $(cat ~/jlog/"$2"); do awk '/'"$1"'/\s=\s[a-zA-Z0-9-]+/ {print}'

And here a piece of the log I am parsing through:

2014-06-17 15:34:52,406 INFO  [random.random.xxx.message.Random] Processed instance (1.2.840.113619.
5/1.2.840.113619.2.80.1944071037.22604.1402323463.136) from random@email.com to Random_AE.  AssociationID = 9eddb1b0-368e-4fb9-a684-44cc14be389c

I am looking specifically for the random code after the Association ID. Is there any suggestions on how to display the results? I am fresh out of ideas.

  • Please try awk '$3=="INFO" && $(NF-2)=="AssociationID" && $(NF-1)=="=" { print $NF }' ~/jlog/server.log. You can directly use awk on your logfile without needing for and cat.
    – user62916
    Jun 18 '14 at 11:28
  • Excellent!! Can you post that as your answer please?
    – ryekayo
    Jun 18 '14 at 11:40

Please try

awk '$3=="INFO" && $(NF-2)=="AssociationID" && $(NF-1)=="=" { print $NF }' ~/jlog/server.log

By passing the logfile's name directly to awk you can avoid using for and cat too.

  • That worked perfectly. Only problem is it is giving me the wrong association ID. Here is the full line of code i have for it: for j in $(cat ~/jlog/"$2"); do awk '$3=="INFO" && $(NF-2)=="AssociationID" && $(NF-1)=="=" { print $NF;exit}' ~/jlog/"$2"
    – ryekayo
    Jun 18 '14 at 11:45
  • Nevermind, found the problem. Thanks!! I was missing a grep statement
    – ryekayo
    Jun 18 '14 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.