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:
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:
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:
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 :
awk '
BEGIN { FS = "\n"
OFS = ","
RS = "eof"
}
{
print $0
}' log2
output:
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:
sed 's/^eof//g' log > log3
and executed the below code :
awk '
BEGIN {FS = "\n"
OFS = ","
RS = ""
}
{trn=$1;nm=$2;tme=$3;msg=$4;
print $1,$2,$3,$4
}' log3
output :
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:
awk '
BEGIN {FS = "\n"
OFS = ","
RS = ""
}
$3 ~ "=07" {
trn=$1;nm=$2;tme=$3;msg=$4;
print $1,$2,$3,$4
}' log3
output:
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