【问题标题】:how to grep for the first instance of the pattern among multiple patterns如何在多个模式中查找模式的第一个实例
【发布时间】:2013-10-06 10:07:59
【问题描述】:

我有三种不同的模式,我想找出文件中最先出现的模式以及文件中最后出现的模式,我还需要打印包含第一个和最后一个模式的行。

我在 grep 下使用,但它仅适用于一种模式。我认为一旦它弄清楚如何找到模式的等级,那么可以使用“tac”通过相同的逻辑来打印最后一个模式。

grep -m 1 "pattern1" test.txt

我的三个模式是

1.PATTERN1  
2.PATTERN2  
3.PATTERN3  

Line1 this is a sample line for example without  any meaning please ignore
Line2 only meant for giving an example PATTERN2 to make my query clear to all
Line3 this is a sample line for example without  any meaning please ignore
Line4 only meant for giving an example pattern1 to make my query clear to all
Line5 this is a sample line for example without  any meaning please ignore
Line6 only meant for giving an example pattern1 to make my query clear to all
Line7 this is a sample line for example without  any meaning please ignore
Line8 only meant for giving an example pattern2 to make my query clear to all
Line9 this is a sample line for example without  any meaning please ignore
Line10 only meant for giving an example pattern3 to make my query clear to all
Line11 only meant for giving an example pattern1 to make my query clear to all

我想打印包含 PATTERN1、PATTERN2、PATTERN3 中任何模式的第一次出现的行。

所以期望的输出应该是:

First pattern among the three
-------------------------------
Line2 only meant for giving an example PATTERN2 to make my query clear to all

Last instance amoung the three:
-------------------------------
Line11 only meant for giving an example pattern1 to make my query clear to all

【问题讨论】:

  • 你能发布一些示例输入和所需的输出吗?你到底想做什么会更清楚。
  • 试图理清思路,请看一下
  • 太好了,现在很清楚了。我稍微格式化了文本。

标签: sed awk grep


【解决方案1】:

你可以说:

grep -E -m1 "pattern1|pattern2|pattern3" test.txt

这将打印匹配pattern1pattern2pattern3 的第一行。

正如您所提到的,您可以使用tac 来查找文件中的最后一个匹配模式:

grep -E -m1 "pattern1|pattern2|pattern3" <(tac test.txt)

如果您的grep 版本不支持-E,您可以说:

grep -m1 "pattern1\|pattern2\|pattern3" test.txt

编辑:为了只找到匹配任何模式的第一行和第一行,你可以说:

grep "pattern1\|pattern2\|pattern3" test.txt | sed -n '1p;$p'

(如果要执行不区分大小写的匹配,请使用 grep-i 选项。)

【讨论】:

  • 谢谢,如果我想删除关于第一个模式的所有内容,如何将 grep 的输出提供给 sed 命令?
  • @user2809888 到 sed 的管道。 | sed '/PATTERN1/d'
  • 我不知道哪个模式会是第一个模式,这是由 grep 的输出决定的。我需要写它,所以 sed 会接受来自 grep/ 的任何输出
  • 更多细节,假设我想删除 PATTERN2 的第一次出现和 pattern1 最后一次出现的所有内容。即使它可能也很奇妙。Pattern2 和 PATTERN1 由输出 og grep 命令决定。
  • 如果您有与新问题无关的问题,请发布新问题。如果您不这么认为,或者编辑您当前的问题。通过 cmets 进行交流对于那些过来寻找类似问题的人来说并没有什么帮助。
【解决方案2】:

使用 for 循环而不是单独的 grep -E 构造。使用 grep -m1,每个模式只返回第一个匹配项。添加了 -i 选项以忽略大小写,否则仅显示完全匹配。

for i in PATTERN1 PATTERN2 PATTERN3; do grep -i $i -m1 test.txt; done

这会产生以下结果...

Line4 only meant for giving an example pattern1 to make my query clear to all
Line2 only meant for giving an example PATTERN2 to make my query clear to all
Line10 only meant for giving an example pattern3 to make my query clear to all

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多