从sed命令获取输出的一部分

时间:2014-01-31 19:36:24

标签: bash sed grep

我有这个命令:

cat -n file.log | grep "Start new test" | tail -1 |  cut -f 1 |  xargs -I % sed -n %',$s/is not alive/&/p' file.log

这给出了整行的输出:

Jan 19 23:20:33 s_localhost@file platMgt.xbin[3260]: blade 10 is not alive
Jan 19 23:20:33 s_localhost@file platMgt.xbin[3260]: blade 11 is not alive

如何修改它才能获得最后一部分: 刀片11不活着

我可以通过显示方式修改它:

Error:blade 11 is not alive ?

感谢您的回复

3 个答案:

答案 0 :(得分:2)

您可以使用剪切在冒号上分隔它,然后添加错误消息:

cat -n file.log | grep "Start new test" | tail -1 |  cut -f 1 |  xargs -I % sed -n %',$s/is not alive/&/p' file.log | cut -d: -f 4 | xargs -I % echo Error: %

答案 1 :(得分:1)

在冒号awk之后得到最后一部分是更好的工具:

s='Jan 19 23:20:33 s_localhost@file platMgt.xbin[3260]: blade 10 is not alive'
awk -F':' '{print "Error:" $NF}' <<< "$s"

输出:

 blade 10 is not alive

编辑:您可以将管道命令组合为:

grep "Start new test" file.log|tail -1|awk -F':' '{print "Error:" $NF}' 

PS:虽然awk本身可以做到这一切。

答案 2 :(得分:0)

以下使用sed命令

获取最后一个“:”分隔字段
cat text.txt | sed 's/^.*: \([^:]*$\)/\1/g'