bzgrep -H pattern file.bz2 gives no filename, but "standard input"

1

I thought -H always should print filename before matched line. this is true for grep, but not for bzgrep:

$ grep -H pattern file1 
file1: data pattern data

$ bzgrep -H pattern file2.bz2 
(standard input): data pattern data

Is there a workaround for this? I need to grep hundreds of bz2 files, I cannot decompress them all before the search...

Washuu

Posted 2014-03-20T11:28:03.273

Reputation: 13

Try specifying multiple files on the command line – golimar – 2014-03-20T11:49:41.190

command line would be too short for that. There are hundreds of files to be searched. I will use bzgrep in a 'for' loop, where variable will be filename taken from a list, as the files are in different directories. – Washuu – 2014-03-20T11:53:57.680

Then create an empty file and grep both the bz2 file and the empty file each time – golimar – 2014-03-20T12:01:10.210

It is something like workaround, thank you. The only drawback is that the filename will be in separate line, what needs to be parsed correctly. I wonder, however, why such bug went unnoticed. Perhaps I have too old version of bzgrep? I will try to test it on newest CentOS. – Washuu – 2014-03-20T12:12:31.750

Answers

0

There is (to my knowledge) no version of grep that internally supports gzip / bzip2. Instead, there exist shell scripts such as bzgrep and zgrep, that essentially call the gunzip / bunzip2 binaries and feed the output into grep via stdin. Therefore, the -H flag cannot work as expected when you don't invoke grep directly.

barbaz

Posted 2014-03-20T11:28:03.273

Reputation: 2 696

0

I looked at bzgrep. It was indeed bash script file. The modification of the script in order to fix the issue was quite simple, so I wondered if someone else corrected it in newer version of bzgrep. I found out, it is corrected on Ubuntu and Debian, but not on CentOS.

If someone stumbles on the same issue - just log in to some newer system, and copy the bzgrep shell script. It's the easiest fix.

Washuu

Posted 2014-03-20T11:28:03.273

Reputation: 13

Could you post the required modification in your answer? – Dennis – 2014-03-24T21:35:00.947