我有三种不同的模式,我想找出文件中最先出现的模式以及文件中最后出现的模式,我还需要打印包含第一个和最后一个模式的行。
我在 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