我有以下内容:
cat /tmp/cluster_concurrentnodedump.out.20140501.103855 | sed -n '/Starting inject/s/.*[Ii]nject \([0-9]*\).*/\1/p
其中列出了
0
1
2
..
如何只使用此sed打印最后一场比赛?
感谢。
答案 0 :(得分:3)
将替换结果存储在保持缓冲区中,然后在结尾处打印:
sed -ne '
/Starting inject/ {
# do the substitution
s/.*[Ii]nject \([0-9]*\).*/\1/
# instead of printing, copy the results to the hold buffer
h
}
$ { # at the end of the file:
# copy the hold buffer back to the pattern buffer
x
# print the pattern buffer
p
}
' /tmp/cluster_concurrentnodedump.out.20140501.103855
答案 1 :(得分:1)
使用tac
反向打印文件(最后一行)并在第一次匹配后退出:
tac /tmp/cluster_concurrentnodedump.out.20140501.103855 | sed -n '/Starting inject/s/.*[Ii]nject \([0-9]*\).*/\1/p;q'
最后一部分是我们退出;q
的地方:
sed -n '....p;q'
^
打印最后一个号码:
$ cat a
1
2
3
4
5
6
7
8
9
$ tac a | sed -n 's/\([0-9]\)/\1/p;q'
9