如何 grep 获取具有特定格式的多行,但如果其中一行有特定的单词,则不包括这些行

How to grep to get multiple lines with a specific format, but if one of the lines has a specific word, do not include the lines as a result

我有一个很大的文件目录,我需要查看其中的特定行,因为它们需要更新。

我要找的格式总是以 或 /> 关闭。

到目前为止,我已经想出了一个正则表达式来查找我想要的行:

pcregrep -HnM '<topicref(.*) href="..\/(.*).dita(.*)[^>]*'

但是,我无法过滤掉具有 scope="peer" 的结果。我试过

pcregrep -HnM '<topicref(.*) href="..\/(.*).dita(.*)[^>]*' directory | pcregrep - Mv 'scope="peer" > file

但是这个结果会严格显示所有不包含 'scope="peer"' 的行,这些行来自上一个 pcregrep 的总体结果,因此会有不应该包括的随机结果,而且我无法跟踪这些结果来自哪些文件。

是否可以在没有 scope="peer" 的情况下看到所有 <topicref href="../... > 提及?

scope="peer" 的三个行示例:

<topicref href="../cat.md" scope="peer"
something />

<topicref href="../cat.md"
something scope="peer"
something />

<topicref href="../cat.md"
scope="peer"
something></topicref><map>

你可以使用

pcregrep -HnM '<topicref(?![^>]*\sscope="peer")(?:\s[^>]+)?\shref="\.\./([^"]*)\.dita[^>]*>' file

详情

  • <topicref - 文字字符串
  • (?![^>]*\sscope="peer") - 在当前位置
  • 右侧除 > 之外的任何零个或多个字符后不允许有空格 + scope="peer"
  • (?:\s[^>]+)? - 一个可选的空格,除 >
  • 之外的一个或多个字符
  • \shref="\.\./ - 空格,href="../ 字符串
  • ([^"]*) - 第 1 组:除 "
  • 以外的零个或多个字符
  • \.dita - .dita 字符串(如果需要匹配 .md 则替换为 \.md
  • [^>]*> - > 以外的零个或多个字符,然后是 >.