Awk modification

I need help modifying the code below.

DATAFILE is a log file.

I have two strings i need to search for in the log file.

The two strings are:

  1. ERROR
  2. com.rolander.promotions.client
awk 'BEGIN {
  while((getline < "'${SFILE}'")>0)
     S[$0]

  FS="\n"; RS="\n"
}

/ERROR/ && /com.rolander.promotions.client/ {

  for(X in D) delete D[X];

  for(N=2; N<=NF; N++)
  {
       gsub(/^[ \t]+/, "", $N);
       split($N, A, "=");
       D[A[1]] = A[2]
       i = 3;
       while (i in A)
          D[A[1]] = D[A[1]] "=" A[i++];
  }

       printf("%s \n")

}' $DATAFILE 

SFILE is suppose to be a file that contains the strings to search for. one string per line. i guess this is redundant since i already hardcoded the strings in the script.

sample log file:

2012-05-30 17:55:58,296 ERROR com.rolander.core.manager.paymentManager.PaymentBL - PaymentBL notifyPaymentOK() method is called. This functionality is not supported.
2012-05-30 17:56:02,855 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception with communication with promotions web service: 
2012-05-30 17:56:02,855 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service host: promo.rolandernetworks.net
2012-05-30 17:56:02,856 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service port: 8080
2012-05-30 17:56:02,856 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service path: /promotions/getReferral
2012-05-30 17:56:02,856 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service params: subAccountNumber=101045334
2012-05-30 17:56:02,856 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service http timeout: 10000
2012-05-30 17:56:02,857 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service tcp timeout: 1000
2012-05-30 17:56:02,857 ERROR com.rolander.core.manager.referralManager.ReferralBL - ERROR getting Referral.

basically, i want the script to scan this file and output ONLY lines that contain the two strings i specified.

awk '/ERROR/ && /com.rolander.promotions.client/' filename

i would like to use the script i pasted because its going about reading the log in a specific way. any ideas?

i've noticed in some cases, its not really what commands you use, its how you use it that will provide the most desired results.

If you told me what you actually wanted, instead of giving me a program which doesn't do what you want and expecting me to infer what you do want somehow, I could do it.

ok.

i have a log file that has data similar to this:

2012-05-30 17:55:58,296 ERROR com.rolander.core.manager.paymentManager.PaymentBL - PaymentBL notifyPaymentOK() method is called. This functionality is not supported.
2012-05-30 17:56:02,855 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception with communication with promotions web service: 
2012-05-30 17:56:02,855 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service host: promo.rolandernetworks.net
2012-05-30 17:56:02,856 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service port: 8080
2012-05-30 17:56:02,856 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service path: /promotions/getReferral
2012-05-30 17:56:02,856 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service params: subAccountNumber=101045334
2012-05-30 17:56:02,856 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service http timeout: 10000
2012-05-30 17:56:02,857 ERROR com.rolander.promotions.client.PromotionsWebServiceClient - Exception web service tcp timeout: 1000
2012-05-30 17:56:02,857 ERROR com.rolander.core.manager.referralManager.ReferralBL - ERROR getting Referral.

Using the script i pasted above, i want to scan this log file for certain strings which must be on the same line.

The file in which the strings to search for are stored is specified with the $SFILE variable, and the strings appear in the file one string per line. in this particular example, the $SFILE variable may not even be needed since I already hardcoded the strings inside the script itself.

so the script is suppose to read in the DATAFILE, and then print out each line it finds that contains both of the specified strings.

So you don't care about the part that does all the g-subbing and splitting for purposes you still haven't disclosed?

awk 'NR==FNR { S[L++]=$0; next } { M=1; for(N=1; M&&(N<=L); N++) if(!(S[N] ~ $0)) M=0 } M' sfile datafile

Then the best script is either coronas script sample.

awk '/ERROR/ && /com.rolander.promotions.client/' DATAFILE

or

awk '/ERROR.*com.rolander.promotions.client/' DATAFILE

please ignore the gsubbing/splitting.

i tested the script, and all it spat out were blank lines.

awk 'NR==FNR { S[L++]=$0; next } { M=1; for(N=1; M&&(N<=L); N++) if(!(S[N] ~ $0)) M=0 } M' sfile /var/tmp/datafile

i would like to see it spit out the lines it found that matched the strings specified in sfile.

if it would be more straightforward, instead of specifying "sfile", can i just specify the two strings on the command line?

something like this:

awk 'NR==FNR { S[L++]=$0; next } { M=1; for(N=1; M&&(N<=L); N++) if(!(S[N] ~ $0)) M=0 } M'  '/ERROR/ && /com.rolander.promotions.client/'  /var/tmp/datafile

:wall:

:wall:

:wall:

awk -v S1="Search1" -v S2="search2" '(S1 ~ $0) && (S2 & $0)' filename

i get this error:

awk -v S1="ERROR" -v S2="PromotionsWebServiceClient" '(S1 ~ $0) && (S2 & $0)' DATAFILE
awk: (S1 ~ $0) && (S2 & $0)
awk:                  ^ syntax error
awk: cmd. line:1: (S1 ~ $0) && (S2 & $0)
awk: cmd. line:1:                       ^ unexpected newline or end of string  

Was already late for corona688:rolleyes:

The var's S1 and S2 need to change position

awk -v S1="ERROR" -v S2="PromotionsWebServiceClient" '($0 ~ S1) && ($0 ~ S2)' DATAFILE > OUTDATAFILE
1 Like

Or?

grep 'ERROR.*com\.rolander\.promotions\.client' infile
1 Like