i need a awk script for reporting purpose, below is the sample output of the log file:
requirement :
we need to find out transaction between 0 to 12 and the expected output should be:
i tried the below code :
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:
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.
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?:
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:
Aplogies for not explaning in a better way.
"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.
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:
because an if statement is not a valid condition.
But, even if we changed:
to:
this is a literal string match; not a regular expression match. And, if we changed it to a regular expression match:
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:
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.
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:
tried your code with some changes:
output:
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 :
output:
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:
and executed the below code :
output :
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:
output:
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.
I'm trying to create a shell script that takes a awk script that I wrote and a filename as an argument. I was able to get that done but I'm having trouble figuring out how to keep the header of the output at the top but sort the rest of the rows alphabetically. This is what I have now but it is... (1 Reply)
Hi
I have a text file (Input.txt) with two column entries separated by tab as given below:
aaa str1
bbb str2
cccccc str3
dddd str4
eee str3
ssss str2
sdf str3
hhh str1
fff str2
ccc str3
.....
.....
..... (1 Reply)
I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist.
diff=$1$2.diff
id=$2 new=new_$diff
echo "My id is $1"
echo "I want to sync for user account $id"
##awk command I am using is as below
cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies
4. Post Here to Contact Site Administrators and Moderators
Variable I have in my shell script
diff=$1$2.diff
id=$2
new=new_$diff
echo "My id is $1"
echo "I want to sync for user account $id"
##awk command I am using is as below
cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new
I could see value of $id is not passing to the awk... (0 Replies)
I am getting the following error while passing parameter to a shell script called within awk script. Any idea what's causing this issue and how to ix it ? Thanks
sh: -c: line 0: syntax error near unexpected token `newline'
sh: -c: line 0: `./billdatecalc.sh ... (10 Replies)
This is the final first release of the dynamic menu generator for pekwm (WM).
#!/bin/bash
function param_val {
awk "/^${1}=/{gsub(/^${1}="'/,""); print; exit}' $2
}
echo "Dynamic {"
for CF in `ls -c1 /usr/share/applications/*.desktop`
do
name=$(param_val Name $CF)
... (3 Replies)
hi everyone
i am trying to do this
bash> cat abc.sh
deepak()
{
echo Deepak
}
deepak
bash>./abc.sh
Deepak
so it is giving me write simply i created a func and it worked
now i modified it like this way
bash> cat abc.sh (2 Replies)
Hello,
I have this awk script that I want to execute by passing parameters through a shell script.
I'm a little confused. This awk script removes duplicates from an input file.
Ok, so I have a .sh file called rem_dups.sh
#!/usr/bin/sh... (4 Replies)
Hi guys
I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file.
#!/bin/sh
echo " $2 $3 $4 $5 $6 $7
isql -w400 -U$2 -S$5 -P$3 << xxx
use $4
go
print"**Changes to the table... (0 Replies)