我有一个名为 compare.txt
的文本文件,我想提取每行中包含模式 nmse_gain_constant
的下一行。以下命令可以接近达成目标:
grep -A 1 nmse_gain_constant compare.txt | grep -v nmse_gain_constant
但是这个包含一个分隔符--
,而且每行所需文字之间都有一条线。有什么简单的方法可以摆脱--
行吗?
例如:对于一个看起来像下面这样的输入文件
line
line
nmse_gain_constant matching line
line after first match
line
line
nmse_gain_constant another matching line
line after second match
line
nmse_gain_constant a third matching line
line after third match
输出结果为
line after first match
--
line after second match
--
line after third match
但我只想要
line after first match
line after second match
line after third match
grep
命令时出现错误:grep: unrecognized option '--no-group-separator'
。 - krookedkingA
标志即可。 - Eddie--no-group-separator
is not supported by all versions ofgrep
- Ericbrew
安装各种命令行工具的Gnu版本,包括grep
。我相信默认情况下它被安装为ggrep
(Gnu Grep)。这支持--no-group-separator
选项。我已经这样做了,它有效。请参考此链接如何在macOS中安装和使用GNU Grep。 - steveb