如何在多个文件中grep的一个模式,而排除注释行

问题描述 投票:0回答:2

我有一堆的,我想用grep pattern *.tex -n --color=always搜索LaTeX文件。重要的是,我看到:

  1. 匹配文件名
  2. 本场比赛的行号
  3. 全行,用匹配的模式凸显

此外,有时将图案需要是一个完整的字匹配,所以命令变为grep pattern *.tex -n -w --color=always

我想修改此命令以排除我*.tex文件,这与%字符开始注释行。我在比赛中的意见不感兴趣。

这可能吗?

regex grep
2个回答
1
投票

可能是你可以试试下面的命令:

grep -n -v --color=always '^%' *.tex | grep content

说明

  • 第一grep调用不包括(-v)开头的行%(正则表达式^%在该行的开头匹配%^))
  • 第一grep调用的输出作为输入被传递到第二grep调用
  • 第二grep调用只包括与过滤模式匹配的行(如果你需要它,你可以添加-w选项)。

我希望这可以帮助你!


0
投票

请问这怎么办?首先是一些测试的东西:

$ cat file
yes
no % yes commented out
yes % yes before comment
no
noyes
furthermore yes
furthermore yes% yes
furthermore no% yes

把手:

$ grep -Hn "^[^%]*\(^\| \)yes\( \|$\|%\)" file
file:1:yes
file:3:yes % yes before comment
file:6:furthermore yes
file:7:furthermore yes% yes

编辑:对于部分匹配:

$ grep -Hn "^[^%]*yes" file 

并强调只有你需要再次用grep它的搜索词:

$ grep -Hn "^[^%]*yes" file | grep yes
file:1:yes
file:3:yes % yes before comment
file:5:noyes
file:6:furthermore yes
file:7:furthermore yes% yes
© www.soinside.com 2019 - 2024. All rights reserved.