awk script: need help

Tags
closed, shell script, shell scripting, shell scripts, unix, unix shell bash scripting

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 07-13-2016
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:
Mod Comment Please use CODE tags for sample input, sample output, AND code segments.

Last edited by Don Cragun; 07-13-2016 at 06:40 PM.. Reason: Add CODE tags.
# 2  
Old 07-13-2016
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

# 3  
Old 07-13-2016
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
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.
awk script: need help-parsing_error-jpg  

# 5  
Old 07-15-2016
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.
# 6  
Old 07-15-2016
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.
# 7  
Old 07-15-2016
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
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk script to call another script based on second column entry my_Perl Shell Programming and Scripting 1 02-08-2016 10:17 AM
awk script genebuster How to Post in the The UNIX and Linux Forums 6 10-08-2014 11:59 AM
Passing shell script parameter value to awk command in side the script Sarita Behera UNIX for Dummies Questions & Answers 1 01-28-2014 05:50 AM
Unable to pass shell script variable to awk command in same shell script Ashunayak Shell Programming and Scripting 2 01-27-2014 01:04 PM
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 11:40 AM
Calling shell script within awk script throws error Sudhakar333 Shell Programming and Scripting 10 05-01-2013 01:04 PM
Passing awk variable argument to a script which is being called inside awk vivek d r Shell Programming and Scripting 2 02-11-2013 07:37 AM
Help: How to convert this bash+awk script in awk script only? alexscript Shell Programming and Scripting 3 02-20-2012 04:25 PM
Help with awk script rover80 Shell Programming and Scripting 3 01-04-2012 12:46 AM
Call shell script function from awk script aishsimplesweet Shell Programming and Scripting 2 08-25-2011 11:42 AM
want to pass parameters to awk script from shell script script_op2a Shell Programming and Scripting 4 11-02-2010 09:37 PM
awk command in script gives error while same awk command at prompt runs fine: Why? catalys Shell Programming and Scripting 4 09-20-2010 11:07 PM
create a shell script that calls another script and and an awk script magikminox Shell Programming and Scripting 0 06-26-2008 03:50 AM
AWK script razoreqx Shell Programming and Scripting 3 08-31-2007 10:28 AM
Convert a script in awk script Lestat Shell Programming and Scripting 14 06-10-2005 11:52 PM
All times are GMT -4. The time now is 04: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?