Phi*_*ide 3 sed text-processing
我可以使用以下命令附加到文件的开头:
sed -i '1s/^/word\n/' file
Run Code Online (Sandbox Code Playgroud)
我在读,如果我使用双引号,我可以扩展变量,所以我尝试:
sed -i "1s/^/$(printenv)\n/" file
Run Code Online (Sandbox Code Playgroud)
我最终回来了:
sed: -e expression #1, char 15: unterminated `s' command
Run Code Online (Sandbox Code Playgroud)
这里发生了什么。它与变量的内容或其他内容有关吗?
我认为以下方法可行:
sed -i '1 e printenv' file
Run Code Online (Sandbox Code Playgroud)
来自 GNU sed 手册:
'e COMMAND'
Executes COMMAND and sends its output to the output stream. The
command can run across multiple lines, all but the last ending with
a back-slash.
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用cat
,但这需要创建一个临时文件:
cat <(printenv) file > temporary_file; mv temporary_file file
Run Code Online (Sandbox Code Playgroud)
如果moreutils
您的机器上安装了该软件包,您可以使用以下命令避免手动创建临时文件sponge
:
cat <(printenv) file | sponge file
Run Code Online (Sandbox Code Playgroud)
在第 1 行之前插入内容:
ed -s file <<< $'0r !printenv\nwq'
Run Code Online (Sandbox Code Playgroud)
在第 1 行后插入内容:
ed -s file <<< $'1r !printenv\nwq'
Run Code Online (Sandbox Code Playgroud)