Greetings, how do I perform the following in BSD sed?
您好,我如何在BSD中执行以下操作?
sed 's/ /\n/g'
From the man-page it states that \n will be treated literally within a replacement string, how do I avoid this behavior? Is there an alternate?
在手册页中,它声明将在替换字符串中处理\n,我如何避免这种行为?有另一个吗?
I'm using Mac OS Snow Leopard, I may install fink to get GNU sed.
我正在使用Mac OS雪豹,我可能会安装fink以获得GNU sed。
4 个解决方案
#1
14
In a shell, you can do:
在壳层中,你可以做到:
sed 's/ /\
/g'
hitting the enter key after the backslash to insert a newline.
在反斜杠后按enter键插入换行。
#3
4
For ease of use, i personally often use
为了便于使用,我个人经常使用
cr="\n"
# or (depending version and OS)
cr="
"
sed "s/ /\\${cr}/g"
so it stays on 1 line.
它在一条直线上。
#4
0
To expand on @sikmir's answer: In Bash, which is the default shell on Mac OS X, all you need to do is place a $
character in front of the quoted string containing the escape sequence that you want to get interpreted. Bash will automatically translate it for you.
要扩展@sikmir的答案:在Bash中,这是Mac OS X上的默认shell,您所需要做的就是在引用的字符串前面加上一个$字符,其中包含您想要解释的转义序列。Bash将自动为您翻译它。
For example, I removed all MS-DOS carriage returns from all the source files in lib/
and include/
by writing:
例如,我删除了lib/中所有源文件的所有MS-DOS回车,并包括/通过写入:
grep -lr $'\r' lib include | xargs sed -i -e $'s/\r//'
find . -name '*-e' -delete
BSD grep
would have interpreted '\r'
correctly on its own, but using $'\r'
doesn't hurt.
BSD grep本身就可以正确地解释“\r”,但是使用$“\r”并不会造成伤害。
BSD sed
would have misinterpreted 's/\r//'
on its own, but by using $'s/\r//'
, I avoided that trap.
BSD sed本来可以自行错误地解释's/\r/ ',但是通过使用$'s/\r/ ' ',我避免了这个陷阱。
Notice that we can put $
in front of the entire string, and it will take care of all the escape sequences in the whole string.
注意,我们可以把$放在整个字符串前面,它将处理整个字符串中的所有转义序列。
$ echo $'hello\b\\world'
hell\world
#1
14
In a shell, you can do:
在壳层中,你可以做到:
sed 's/ /\
/g'
hitting the enter key after the backslash to insert a newline.
在反斜杠后按enter键插入换行。
#2
#3
4
For ease of use, i personally often use
为了便于使用,我个人经常使用
cr="\n"
# or (depending version and OS)
cr="
"
sed "s/ /\\${cr}/g"
so it stays on 1 line.
它在一条直线上。
#4
0
To expand on @sikmir's answer: In Bash, which is the default shell on Mac OS X, all you need to do is place a $
character in front of the quoted string containing the escape sequence that you want to get interpreted. Bash will automatically translate it for you.
要扩展@sikmir的答案:在Bash中,这是Mac OS X上的默认shell,您所需要做的就是在引用的字符串前面加上一个$字符,其中包含您想要解释的转义序列。Bash将自动为您翻译它。
For example, I removed all MS-DOS carriage returns from all the source files in lib/
and include/
by writing:
例如,我删除了lib/中所有源文件的所有MS-DOS回车,并包括/通过写入:
grep -lr $'\r' lib include | xargs sed -i -e $'s/\r//'
find . -name '*-e' -delete
BSD grep
would have interpreted '\r'
correctly on its own, but using $'\r'
doesn't hurt.
BSD grep本身就可以正确地解释“\r”,但是使用$“\r”并不会造成伤害。
BSD sed
would have misinterpreted 's/\r//'
on its own, but by using $'s/\r//'
, I avoided that trap.
BSD sed本来可以自行错误地解释's/\r/ ',但是通过使用$'s/\r/ ' ',我避免了这个陷阱。
Notice that we can put $
in front of the entire string, and it will take care of all the escape sequences in the whole string.
注意,我们可以把$放在整个字符串前面,它将处理整个字符串中的所有转义序列。
$ echo $'hello\b\\world'
hell\world