Developer
Developer

Reputation: 6350

Grep data from a zipped file Unix

I have a file having data pipe separated , i want to get a grep a text between 2 times . Below is the file structure

2017-04-23 06:43:00|1|2|3|4|5|TEST|
2017-04-23 06:43:01|1|2|3|4|5|TEST|
2017-04-23 06:43:02|1|2|3|4|5|A|
2017-04-23 06:44:00|1|2|3|4|5|TEST|

Now i want to count occurrence of TEST between 06:43:00 to 06:44:00.I tired using grep

zgrep -a "06:43:00" filename.gz | grep "TEST" | wc -l

But i am unable to get the count between the 2 times mentioned .

Upvotes: 0

Views: 130

Answers (2)

Essex Boy
Essex Boy

Reputation: 7950

Use expression grep (egrep) to grep for more than one pattern

zegrep '06:43|06:44:00' filename.gz | grep TEST | wc -l

or

zcat filename.gz | egrep '06:43|06:44:00' | grep TEST | wc -l

$ zcat file1.gz | egrep '06:43|06:44:00' | grep TEST
2017-04-23 06:43:00|1|2|3|4|5|TEST|
2017-04-23 06:43:01|1|2|3|4|5|TEST|
2017-04-23 06:44:00|1|2|3|4|5|TEST|

$ zcat file1.gz | egrep '06:43|06:44:00' | grep TEST | wc -l
3

Or very hacky :

  zcat file1.gz | perl -ne 'if(/06:43:00/){$p=1;}if($p){print $_;}if(/06:44:00/){$p=0;}' | grep TEST | wc -l    3

Upvotes: 1

ULick
ULick

Reputation: 999

zcat filename.gz \
    | awk '/06:43/,/06:44/ { print }' - \
    | grep TEST | wc -l

awk usually uses /<pattern>/ { <action> }, but it is also possible to define a range with two comma separated patterns. /<start-pattern>/,/<end-pattern/ { <action> }

Upvotes: 0

Related Questions