sed-在文件的最后一行之前插入字符串 [英] sed - Piping a string before the last line in a file

查看:103
本文介绍了sed-在文件的最后一行之前插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条只打印一行的命令.我想将此行添加/管道到文件的最后一行上方.

I have a command that prints a single line. I want to add/pipe this line to a file, just above its last line.

my_cmd | sed -i '$i' test

我只是在最后一行上方的正确位置找到一个空行.我注意到,当我将任何字符串添加为'$ i foo'时,"foo"都将打印在正确的位置,但是我希望打印管道线.

I just find an empty line in the correct place, above the last line. I notice that when I add any string as '$i foo', the "foo" gets printed in the correct place, but I want the piped line to be printed.

如何使用STDIN代替"foo"?

How can I use STDIN instead of "foo"?

推荐答案

这应该可以解决问题:

 sed -i "\$i $(cmd)" file

测试:

kent$  cat f
1
2
3
4
5

kent$  sed -i "\$i $(date)" f

kent$  cat f
1
2
3
4
Tue Sep 30 14:10:02 CEST 2014
5

这篇关于sed-在文件的最后一行之前插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆