regex - Only print records that have a matching field after using a record separator -


i have bunch of records, , want see records field (or multiple fields) match regex pattern.

for example, data comes in form this:

time=181 ms requestid=12345 method=get ... # other records endtime=tue mar 5 16:21:03 2015 utc eoe ------------------------------------------------------------------------ time=4003 ms requestid=53224 method=post ... # other records endtime=tue mar 5 16:21:09 2015 utc eoe ------------------------------------------------------------------------ 

the way have done before use grep -a , -b flags extract context.

zgrep "16:2[0-5]:" -a 1 -b 10 data.txt 

this works fine if looking single pattern , each record has same amount of lines, becomes more complicated once have multiple fields own patterns.

what want separate each 1 of these inputs record , print each record of 1 or more of fields match pattern looking for.

i can split them records using rs, how print records have different parts matching? if wanted find records have endtime between 16:20 , 16:30 , time longer 1 second ([0-9]{4,}) when there arbitrary amount of rows in between?

awk 'begin { rs = "eoe" } { ??? }' data.txt 

it sounds main issue how access data, not how test after you've got it, so: whenever have name=value pairs in input it's idea construct name2value array , can access values name:

$ cat tst.awk begin { rs="\neoe\n-+\n"; fs="\n"; ofs="," } {     delete n2v     (i=1;i<=nf;i++) {         name = gensub(/=.*$/,"","",$i)         value = gensub(/[^=]+=/,"","",$i)         n2v[name] = value     }     print n2v["time"], n2v["requestid"], n2v["method"], n2v["endtime"] }  $ awk -f tst.awk file 181 ms,12345,get,tue mar 5 16:21:03 2015 utc 4003 ms,53224,post,tue mar 5 16:21:09 2015 utc 

the above uses gnu awk extensions. use gawk time functions time math need do.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -