4

I am using zgrep to search a pattern and print N lines after the matched line from multiple .gz files.

In BSD zgrep I see a separator -- after each file:

zgrep (BSD grep) 2.5.1-FreeBSD
$ zgrep  "match string" -A 1 FileName1.log.gz FileName2.log.gz
FileName1.log.gz:match string
FileName1.log.gz-
--
FileName2.log.gz:match string
FileName2.log.gz-

But in GNU I do not see the same:

zgrep (gzip) 1.6
Copyright (C) 2010-2013 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.

# zgrep  "match string" -A 1 FileName1.log.gz FileName2.log.gz
FileName1.log.gz:match string
FileName1.log.gz:
FileName2.log.gz:match string
FileName2.log.gz:

I tried --group-separator but it seems to delimit for matches in single file.

How can I obtain a similar result in GNU zgrep?

1

You can not get a similar output with GNU zgrep (or OpenBSD zgrep for that matter, it seems to be particular to zgrep on FreeBSD and macOS). Not unless you do your own post-processing of the results, which is easy:

$ zgrep hello file*.gz | awk -F : 'NR > 1 && $1 != name { print "--" } { name = $1; print }'
file1.gz:hello world
--
file2.gz:hello world
--
file3.gz:hello world
--
file4.gz:hello world
--
file5.gz:hello world

This passes the output of zgrep through a short awk program that will insert the separator whenever the filename bit of the line (the bit before the first :) changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.