在 Unix 上结合 echo 和 cat
非常简单的问题,如何在 shell 中组合 echo 和 cat,我试图将一个文件的内容写入另一个带有前置字符串的文件?
如果 /tmp/file 看起来像这样:
this is a test
我想运行这个:
echo "PREPENDED STRING"
cat /tmp/file | sed 's/test/test2/g' > /tmp/result
这样 /tmp/result 看起来像这样:
PREPENDED STRINGthis is a test2
谢谢。
Really simple question, how do I combine echo and cat in the shell, I'm trying to write the contents of a file into another file with a prepended string?
If /tmp/file looks like this:
this is a test
I want to run this:
echo "PREPENDED STRING"
cat /tmp/file | sed 's/test/test2/g' > /tmp/result
so that /tmp/result looks like this:
PREPENDED STRINGthis is a test2
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这应该有效:
This should work:
尝试:
括号在子 shell 内运行命令,以便输出看起来像
>/tmp/result
重定向的单个流。Try:
The parentheses run the command(s) inside a subshell, so that the output looks like a single stream for the
>/tmp/result
redirect.或者只使用 sed
Or just use only sed
或者也可以:
Or also:
如果这是用于发送电子邮件,请记住使用 CRLF 行结尾,如下所示:
注意字符串内的 -e 标志和 \r。
设置为:这种循环方式将为您提供世界上最简单的批量邮件程序。
If this is ever for sending an e-mail, remember to use CRLF line-endings, like so:
Notice the -e-flag and the \r inside the string.
Setting To: this way in a loop gives you the world's simplest bulk-mailer.
另一种选择:假设前置字符串只应出现一次,而不是每一行:
Another option: assuming the prepended string should only appear once and not for every line: