Dan*_*Dan 31 unix shell piping
真的很简单的问题,如何在shell中组合echo和cat,我正在尝试将文件的内容写入另一个带有前置字符串的文件中?
如果/ tmp/file看起来像这样:
this is a test
Run Code Online (Sandbox Code Playgroud)
我想运行这个:
echo "PREPENDED STRING"
cat /tmp/file | sed 's/test/test2/g' > /tmp/result
Run Code Online (Sandbox Code Playgroud)
所以/ tmp/result看起来像这样:
PREPENDED STRINGthis is a test2
Run Code Online (Sandbox Code Playgroud)
谢谢.
Dou*_*las 38
这应该工作:
echo "PREPENDED STRING" | cat - /tmp/file | sed 's/test/test2/g' > /tmp/result
Run Code Online (Sandbox Code Playgroud)
Gre*_*ill 11
尝试:
(printf "%s" "PREPENDED STRING"; sed 's/test/test2/g' /tmp/file) >/tmp/result
Run Code Online (Sandbox Code Playgroud)
括号在子shell中运行命令,因此输出看起来像>/tmp/result
重定向的单个流.
归档时间: |
|
查看次数: |
41298 次 |
最近记录: |