IMPe
May 16, 2013, 12:32pm
1
Hi,
i have some problems with regular expressions in a awk search pattern.
what i want to do:
i want to calculate the mean-value in the time from 00:00 to 06:00
how my data looks like:
....
04/01/13-01:40 670992 54802 80711 116460 156177
04/01/13-01:50 703725 60150 85498 118844 154391
04/01/13-02:00 639716 54841 79710 112262 153321
.....
what i tried so far:
i use the timestamp 04/01/13-hh:mm to filter my calculation.
the calc of the mean over the whole day works fine with
awk \04/01/13\ '{a+=$2} END{print a/NF}' file.txt
but when i try to extend the pattern like ..
awk \04/01/13-0[0-5]\ '{a+=$2} END{print a/NF}' file.txt
awk will crash.
can someone give me a hint please.
Thanks in advance!
IMPe
What do you mean 'awk will crash'?
I'm amazed these worked either way honestly -- you can't have code outside the quote like that...
awk '/04\/01\/13-0[0-5]/ { a+=$2 ; b++ } END { print a/b }' file.txt
1 Like
Yoda
May 16, 2013, 12:43pm
3
You have to escape the forward slash:
awk '/04\/01\/13-0[0-5]/{a+=$2;n=NF}END{print a/n}' file.txt
1 Like
IMPe
May 17, 2013, 2:41am
4
Thanks a lot to both of you, corona688 and yoda.
Now it works fine, but when i try to change my time in the searchpattern, i.e. from 06-11
[ to take the data in between 06:00 - 12:00 ] i struggle again!
Can you please give me one more helping hand?
Thanks
IMPe
---------- Post updated at 01:41 AM ---------- Previous update was at 12:03 AM ----------
impe:
Thanks a lot to both of you, corona688 and yoda.
Now it works fine, but when i try to change my time in the searchpattern, i.e. from 06-11
[ to take the data in between 06:00 - 12:00 ] i struggle again!
Can you please give me one more helping hand?
i think i found a way.
putting a seperate beginning and a end of the reg-expression ...
awk '/04\/01\/13-00/,/04\/01\/13-05:50/ {a+=$2;n=NF}END{print a/n}' file.txt
.... it seems to work fine.