Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk script: need help


👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-13-2016
bhupeshchavan bhupeshchavan is offline
Registered User
 
Join Date: Jun 2014
Last Activity: 5 October 2016, 11:15 AM EDT
Location: Mumbai,India
Posts: 16
Thanks: 6
Thanked 0 Times in 0 Posts
awk script: need help

Hi Team,

i need a awk script for reporting purpose, below is the sample output of the log file:
Code:
transid=01
name=admin
time=06.58.51
message=test
eof
transid=02
name=account
time=14.58.51
message=live
eof
transid=03
name=bhu3
time=07.58.51
message=testing
eof

requirement :
we need to find out transaction between 0 to 12 and the expected output should be:
Code:
 transid=03, name=bhu3, time=07.58.51, message=testing

i tried the below code :
Code:
awk 'BEGIN{FS="\n";RS="eof";trn=$1;nm=$2;tme=$3;msg=$4;} if($3=="^[0-1][0-9]") {print trn","nm","tme","msg}' log

but it is not working i am getting parsing error. I tried several different combination but none of them worked.

please help.

Thank you in advance.

Thanks and regards,
Bhupesh
Moderator's Comments:
awk script: need help Please use CODE tags for sample input, sample output, AND code segments.

Last edited by Don Cragun; 07-13-2016 at 05:40 PM.. Reason: Add CODE tags.
Sponsored Links
    #2  
Old 07-13-2016
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 19 July 2018, 12:35 PM EDT
Location: San Jose, CA, USA
Posts: 11,408
Thanks: 651
Thanked 3,970 Times in 3,393 Posts
What operating system are you using. According to the standards, the record separator is a single character (not a string like eof, although some versions of awk do accept an extended regular expression instead of just a single character).

It is nice of you to tell us that you are getting a parsing error. But, it would be a lot more informative if you showed us the diagnostic messages awk was producing (in CODE tags) instead of just saying there is an error.

Don't transid=01 and transid=02 also indicate transactions between 0 and 12? Why isn't the desired output?:
Code:
transid=01,name=admin,time=06.58.51,message=test
transid=02,name=account,time=14.58.51,message=live
transid=03,name=bhu3,time=07.58.51,message=testing

Sponsored Links
    #3  
Old 07-13-2016
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 31 May 2018, 9:41 AM EDT
Location: Houston, Texas, USA
Posts: 1,142
Thanks: 0
Thanked 421 Times in 398 Posts
Code:
awk -F= '
$1 == "transid" {tid=$2}
$1 == "eof" && (tid >=0 && tid <=12) {sub(" *, *$", "", l); print l; l=""; next}
{l=l $0 ", "}
' log

    #4  
Old 07-14-2016
bhupeshchavan bhupeshchavan is offline
Registered User
 
Join Date: Jun 2014
Last Activity: 5 October 2016, 11:15 AM EDT
Location: Mumbai,India
Posts: 16
Thanks: 6
Thanked 0 Times in 0 Posts
Hi Don,

Please find the details required:

Code:
[bhupesh@RHL9 bhupesh]$ ls -ltr `which awk`
lrwxrwxrwx    1 root     root           14 Oct  9  2011 /usr/bin/awk -> ../../bin/gawk
[bhupesh@RHL9 bhupesh]$ uname -a
Linux RHL9.0 2.4.20-31.9 #1 Tue Apr 13 18:04:23 EDT 2004 i686 i686 i386 GNU/Linux
[bhupesh@RHL9 bhupesh]$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
[bhupesh@RHL9 bhupesh]$
[bhupesh@RHL9 bhupesh]$ awk 'BEGIN{FS="\n";RS="eof";trn=$1;nm=$2;tme=$3;msg=$4;} if($3=="^[0-1][0-9]") {print trn","nm","tme","msg}' log
awk: cmd. line:1: BEGIN{FS="\n";RS="eof";trn=$1;nm=$2;tme=$3;msg=$4;} if($3=="^[0-1][0-9]") {print trn","nm","tme","msg}
awk: cmd. line:1:                                                     ^ parse error
[bhupesh@RHL9 bhupesh]$

Don't transid=01 and transid=02 also indicate transactions between 0 and 12? Why isn't the desired output?:
--> To avoid the script consedering transid=01 and transid=02,i am trying to use FS as new line and RS as eof, and then i am using "if($3=="^[0-1][0-9]")" so that awk can only check the 3rd field and in that 3rd feild pattern is "time=[0-1][0-9]". I do apologize i did not put time=[0-1][0-2] in the first place.

The output should be genarted based on values in the third feild i.e time=07.58.51, in this feild it should only consider the hour range, which is seven in this feild.

Enclosed is the snap of the same.

I did one more attempt at it but output is same:
code:
Code:
awk 'BEGIN{FS="\n";RS="eof";trn=$1;nm=$2;tme=$3;msg=$4;} if($3=="time=[0-1][0-9]*") {print trn","nm","tme","msg}' log

Aplogies for not explaning in a better way.

Thank you in advance.
Attached Thumbnails
awk script: need help-parsing_error-jpg  
Sponsored Links
    #5  
Old 07-15-2016
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 July 2018, 7:29 AM EDT
Location: Aachen, Germany
Posts: 13,075
Thanks: 449
Thanked 4,016 Times in 3,693 Posts
Hmmm - where to start whining?
  • "transaction between 0 to 12" doesn't mean 0 <= transid <= 12 but the hour of the entry's time? Why, then, is entry 1 missing in your output?
  • in your text, you specify time=[0-1][0-2] which evaluates to 00, 01, 02, 10, 11, 12. In your script sample, you write time=[0-1][0-9] which evaluates to 00 - 19, neither of which may be what you really want.
  • the parser error is due to the if in the pattern part of the awk command is illegal. Either use without if, which is possible in an awk pattern {action} pair, or put it within the curly braces.
  • don't attach pictures - data in there can't be copied and analysed but only visually interpreted.
Sponsored Links
    #6  
Old 07-15-2016
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 19 July 2018, 12:35 PM EDT
Location: San Jose, CA, USA
Posts: 11,408
Thanks: 651
Thanked 3,970 Times in 3,393 Posts
Expanding on what RudiC has already said...

Your first post said absolutely nothing about time of day having any effect on the transactions that should be displayed; it said the only requirement was that the transaction ID should be between 00 and 12.

I know that many people like to write awk one-liners, instead of writing awk scripts such that you can see the structure of the code and easily spot cases where you have invalid conditions in an awk:
Code:
condition { action }

because an if statement is not a valid condition.

But, even if we changed:
Code:
if($3=="time=[0-1][0-9]*") {print trn","nm","tme","msg}

to:
Code:
$3=="time=[0-1][0-9]*" {print trn","nm","tme","msg}

this is a literal string match; not a regular expression match. And, if we changed it to a regular expression match:
Code:
i$3~"time=[0-1][0-9]*" {print trn","nm","the","msg}

that would match transactions that had a time value starting with any value in the range 00 through 19, inclusive.

And, you have another logic error, because $1, $2, $3, and $4 do not have any defined value before the 1st input line has been read (and the BEGIN clause in your awk script is executed before the 1st line of input is read).

If the transaction ID doesn't matter and the only selection criteria for printing records is that the time value starts with 07, you might want something more like:
Code:
awk '
BEGIN {	FS = "\n"
	OFS = ","
	RS = "eof"
}
$3 ~ "=07" {
	print $1, $2, $3, $4
}' log

although this is untested (since the awk on my system does not support multi-character record separators) and it isn't obvious to me whether the <newline> following the eof record separator produces an empty 1st field in records after the 1st record.
Sponsored Links
    #7  
Old 07-15-2016
bhupeshchavan bhupeshchavan is offline
Registered User
 
Join Date: Jun 2014
Last Activity: 5 October 2016, 11:15 AM EDT
Location: Mumbai,India
Posts: 16
Thanks: 6
Thanked 0 Times in 0 Posts
Hi Don,

Your comments gave me some hint where i was going wrong, to remove confusion of RS and to check if the "record separator produces an empty 1st field in records after the 1st record", i first changed the file to a different format.

Test file 1 : filename is log2:
Code:
transid=01,name=bhu,time=06.58.51,message=testeoftransid=2,name=account,time=14.58.51,message=liveeoftransid=3,name=bhu3,time=07.58.51,message=testingeof

tried your code with some changes:
Code:
awk '
BEGIN {    FS = "\n"
    OFS = ","
    RS = "eof"
}
#$3 ~ "=07" { --> commented out this part since i wanted to check if the formatting is perfect  or not.
{trn=$1;nm=$2;tme=$3;msg=$4;   #declared it in action pattern.
print trn "," nm "," tme "," msg
}' log2 

and

awk '
BEGIN {FS = "\n"
OFS = ","
RS = "eof"
}
{
print $1, $2, $3, $4
}' log2

output:
Code:
transid=01,name=bhu,time=06.58.51,message=test,,,,
transid=2,name=chu,time=14.58.51,message=test,,,,
transid=3,name=bhu3,time=07.58.51,message=test,,,,
,,,

This worked as expected but the only area of concern in this output is the commas after the last field and the last line of the output with commas, so i tried the
below code:

If i ignore variable part and execute the below code :
Code:
awk '
BEGIN {    FS = "\n"
    OFS = ","
    RS = "eof"
}
 {
    print $0
}' log2

output:
Code:
transid=01,name=bhu,time=06.58.51,message=test
transid=2,name=account,time=14.58.51,message=live
transid=3,name=bhu3,time=07.58.51,message=testing

There are two blank lines at the end in this output.



Then i went ahead one more step and replaced eof with nothing with the help of below command:
Code:
sed 's/^eof//g' log > log3

and executed the below code :
Code:
awk '
BEGIN {FS = "\n"
OFS = ","
RS = ""
}
 {trn=$1;nm=$2;tme=$3;msg=$4;
print $1,$2,$3,$4
}' log3

output :
Code:
transid=01,name=admin,time=06.58.51,message=test
transid=02,name=account,time=14.58.51,message=live
transid=03,name=bhu3,time=07.58.51,message=testing

Output is fine now.

The only thing which was left is evaluating time frame in the third feild(Transaction which are in time frame 00 to 12) but before doing this i tried the $3 ~ 07 and it worked:
Code:
awk '
BEGIN {FS = "\n"
OFS = ","
RS = ""
}
$3 ~ "=07" {
 trn=$1;nm=$2;tme=$3;msg=$4;
print $1,$2,$3,$4
}' log3

output:
Code:
transid=03,name=bhu3,time=07.58.51,message=testing

I tried different patterns but was not able to write between statement to get transaction between 00 to 12,please help me out in this.


Hi RudiC,

"transaction between 0 to 12" doesn't mean 0 <= transid <= 12 but the hour of the entry's time? Why, then, is entry 1 missing in your output?
--Yes, the first entry was missing.

in your text, you specify time=[0-1][0-2] which evaluates to 00, 01, 02, 10, 11, 12. In your script sample, you write time=[0-1][0-9] which evaluates to 00 - 19, neither of which may be what you really want.
--> This is my mistake,apologies for the same. I am looking to get transaction which falls under time frame 00-12 and the time is in the 3rd feild.

the parser error is due to the if in the pattern part of the awk command is illegal. Either use without if , which is possible in an awk pattern {action} pair, or put it within the curly braces.
--> Please give me an example so that i will understand better,i am not good in awk still learning and exploring.

I have tried achieve the output but i am stuck in matching the time range.

Thank you in advance

Regards,
Bhupesh
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing shell script parameter value to awk command in side the script Sarita Behera UNIX for Dummies Questions & Answers 1 01-28-2014 04:50 AM
Unable to pass shell script parameter value to awk command in side the same script Ashunayak Post Here to Contact Site Administrators and Moderators 0 01-27-2014 10:40 AM
Passing awk variable argument to a script which is being called inside awk vivek d r Shell Programming and Scripting 2 02-11-2013 06:37 AM
Help: How to convert this bash+awk script in awk script only? alexscript Shell Programming and Scripting 3 02-20-2012 03:25 PM
create a shell script that calls another script and and an awk script magikminox Shell Programming and Scripting 0 06-26-2008 02:50 AM



All times are GMT -4. The time now is 03:18 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?