如何grep最后一次出现的线条图案 [英] How to grep the last occurrence of a line pattern
本文介绍了如何grep最后一次出现的线条图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含内容的文件
I have a file with contents
x
a
x
b
x
c
我想 grep 最后一次出现,
I want to grep the last occurrence,
x
c
当我尝试
sed -n "/x/,/b/p" file
它列出了所有行,从 x
到 c
.
it lists all the lines, beginning x
to c
.
推荐答案
我不确定我的问题是否正确,所以这里有一些黑暗中的镜头:
I'm not sure if I got your question right, so here are some shots in the dark:
打印
x
的最后一次出现(正则表达式):
Print last occurence of
x
(regex):
grep x file | tail -1
或者:
Alternatively:
tac file | grep -m1 x
从匹配的第一行到结尾打印文件:
Print file from first matching line to end:
awk '/x/{flag = 1}; flag' file
从最后一个匹配行到结尾打印文件(在不匹配的情况下打印所有行):
Print file from last matching line to end (prints all lines in case of no match):
tac file | awk '!flag; /x/{flag = 1};' | tac
这篇关于如何grep最后一次出现的线条图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文