【发布时间】:2018-05-29 07:45:51
【问题描述】:
我对 linux 比较陌生,我想在文件中搜索以“Leonard is”开头并以“champion”结尾的模式
这个模式也可以放在多行中
输入文件(input.txt)可能如下所示:
1 rabbit eats carrot Leonard is a champion
2 loin is the king of
3 jungle Leonard is a
4 Champion
5 Leonard is An exemplary
6 Champion
我希望我的模式的所有出现都忽略输出文件中模式以外的所有其他字符:
1 Leonard is a champion
3 Leonard is a
4 Champion
5 Leonard is An exemplary
6 Champion
我已经非常接近以下命令:
cat input.txt | grep -ioE "Leonard.*Champion$"
因为这个命令只返回
1 Leonard is a champion
忽略多行中出现的所有模式
如果 grep 以外的任何其他搜索方法有用,请告诉我谢谢!
【问题讨论】:
-
重新格式化做得很好! :)
-
试试
grep -iPoz "(?m)Leonard.*?Champion$"(如果您使用的是GNUgrep)