我正在使用zgrep搜索一个模式,并在多个.gz文件的匹配行之后打印N行。
在BSD中,我在每个文件之后看到一个分隔符--:
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-但在GNU中,我看到的不一样:
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 .
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:我试过--组分隔符,但它似乎在单个文件中分隔匹配。
如何在GNU zgrep中获得类似的结果?
发布于 2019-07-27 08:28:25
用GNU zgrep (或OpenBSD zgrep )无法获得类似的输出,这似乎是zgrep on FreeBSD和macOS特有的)。除非您自己对结果进行后处理,这很容易:
$ 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这会将zgrep的输出传递给一个简短的awk程序,每当行的文件名位(第一个:之前的位)发生变化时,该程序就会插入分隔符。
https://unix.stackexchange.com/questions/530120
复制相似问题