如何使用shell脚本在条件满足后打印下一行?

时间:2015-05-25 12:56:52

标签: linux shell sed

我有以下格式的数据文件。

file.dat
the value of abc is
56
the value of xyz is
60
the value of pqr is
54
the value of xyz is
78
.......

我的输出将是

file.dat
56
60
54
78

我使用以下方法,但它看起来不合适。

sed -i 's/^the/d' file.dat
sed -i 's/^$/d' file.dat

还有其他好办法吗?

我还想只打印xyz值。喜欢:如果该行包含xyz,则打印下一行。

file.dat 
60
78

1 个答案:

答案 0 :(得分:1)

我会说

sed -n '/the value of xyz/ { n; p; }' file.dat

此操作如下:-n禁用自动打印(以便n不打印任何内容),代码为

/the value of xyz/ {   # if a line contains "the value of xyz"
  n                    # fetch the next line
  p                    # and print it
}

由您决定条件有多严格。我选择了一个看起来像是一个理智的选择器给我;当然,您可以替换任何可靠地识别您想要的行的正则表达式。