如何在逗号后插入换行符`),(` sed? [英] How to insert newline character after comma in `),(` with sed?
问题描述
如何在 ),(
中用 sed 在逗号后插入换行符?
How to insert newline character after comma in ),(
with sed?
$ more temp.txt
(foo),(bar)
(foobar),(foofoobar)
$ sed 's/),(/),\n(/g' temp.txt
(foo),n(bar)
(foobar),n(foofoobar)
为什么这不起作用?
推荐答案
好吧,我知道这个问题很老了,但我不得不涉足这个问题以使 sed 接受 \n 字符.我找到了一个适用于 bash 的解决方案,我在此为遇到相同问题的其他人指出.
OK, I know this question is old but I just had to wade trough this to make sed accept a \n character. I found a solution that works in bash and I am noting it here for others who run into the same problem.
重申问题:让 sed 接受反斜杠转义换行符(或其他反斜杠转义字符).
To restate the problem: Get sed to accept the backslash escaped newline (or other backslash escaped characters for that matter).
bash 中的解决方法是使用:
The workaround in bash is to use:
$'\n'
在 bash 中,$'\n' 字符串被替换为真正的换行符.
In bash a $'\n' string is replaced with a real newline.
您唯一需要做的另一件事是对 \n 进行双重转义,因为您必须对斜杠本身进行转义.
The only other thing you need to do is double escape the \n as you have to escape the slash itself.
综合起来:
sed $'s/),(/),\\\n(/g' temp.txt
(foo),
(bar)
(foobar),
(foofoobar)
如果您希望它实际更改而不是打印出来,请使用 -i
If you want it actually changed instead of being printed out use the -i
sed -i $'s/),(/),\\\n(/g' temp.txt
这篇关于如何在逗号后插入换行符`),(` sed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!