如何在逗号后用逗号插入换行符),(?
$ more temp.txt
(foo),(bar)
(foobar),(foofoobar)
$ sed 's/),(/),\n(/g' temp.txt
(foo),n(bar)
(foobar),n(foofoobar)
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?
我想使用 sed 在文件中的目标行上方插入多行。
下面file.txt包含一行“ target line”。我的初始版本使用单引号:
sed '/target line/ i\
inserted line1;\
inserted line2;\
inserted line3;' file.txt
Run Code Online (Sandbox Code Playgroud)
结果是:
inserted line1;
inserted line2;
inserted line3;
target line
Run Code Online (Sandbox Code Playgroud)
此版本按预期工作,每行末尾的换行符被转义为\文字换行符而不是命令终止符。请参阅此处。
然后我想在替换字符串中使用 shell 变量,因此我尝试使用双引号来启用变量扩展:
sed "/target line/ i\
inserted line1;\
inserted line2;\
inserted line3;" file.txt
Run Code Online (Sandbox Code Playgroud)
但这一次换行符和前四个空格消失了:
inserted line1; inserted line2; inserted line3;
target line
Run Code Online (Sandbox Code Playgroud)
如何在此处正确插入双引号中的换行符?