如何在 BSD sed 中使用换行符替换?

发布于 2024-08-05 12:06:53 字数 185 浏览 11 评论 0原文

您好,我如何在 BSD sed 中执行以下操作?

sed 's/ /\n/g'

从手册页来看,它指出 \n 将在替换字符串中按字面意思处理,如何避免这种行为?有替代方案吗?

我使用的是 Mac OS Snow Leopard,我可以安装 fink 来获取 GNU sed。

Greetings, how do I perform the following in BSD sed?

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?

I'm using Mac OS Snow Leopard, I may install fink to get GNU sed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉生梦死 2024-08-12 12:06:53

在 shell 中,您可以执行以下操作:

    sed 's/ /\
/g'

在反斜杠后面按 Enter 键以插入换行符。

In a shell, you can do:

    sed 's/ /\
/g'

hitting the enter key after the backslash to insert a newline.

未央 2024-08-12 12:06:53

另一种方式:

sed -e 's/ /\'

请参阅此处

\n/g'

请参阅此处

Another way:

sed -e 's/ /\'

See here.

\n/g'

See here.

哆兒滾 2024-08-12 12:06:53

为了方便使用,我个人经常使用,

cr="\n" 
# or (depending version and OS)
cr="
"

sed "s/ /\\${cr}/g"

所以它保持在1行。

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.

余厌 2024-08-12 12:06:53

扩展@sikmir的答案:在Bash(Mac OS X上的默认shell)中,您需要做的就是在包含您想要的转义序列的引用字符串前面放置一个 $ 字符得到解释。 Bash 会自动为你翻译。

例如,我通过编写以下内容从 lib/include/ 中的所有源文件中删除了所有 MS-DOS 回车符:

grep -lr 

BSD grep 将会有单独正确解释 '\r' ,但使用 $'\r' 不会有什么坏处。

BSD sed 本身会误解 's/\r//',但使用 $'s/\r//' ,我避开了那个陷阱。

请注意,我们可以将 $ 放在整个字符串的前面,它将处理整个字符串中的所有转义序列。

$ echo 
\r' lib include | xargs sed -i -e 

BSD grep 将会有单独正确解释 '\r' ,但使用 $'\r' 不会有什么坏处。

BSD sed 本身会误解 's/\r//',但使用 $'s/\r//' ,我避开了那个陷阱。

请注意,我们可以将 $ 放在整个字符串的前面,它将处理整个字符串中的所有转义序列。


s/\r//'
find . -name '*-e' -delete

BSD grep 将会有单独正确解释 '\r' ,但使用 $'\r' 不会有什么坏处。

BSD sed 本身会误解 's/\r//',但使用 $'s/\r//' ,我避开了那个陷阱。

请注意,我们可以将 $ 放在整个字符串的前面,它将处理整个字符串中的所有转义序列。


hello\b\\world'
hell\world
\r' lib include | xargs sed -i -e

BSD grep 将会有单独正确解释 '\r' ,但使用 $'\r' 不会有什么坏处。

BSD sed 本身会误解 's/\r//',但使用 $'s/\r//' ,我避开了那个陷阱。

请注意,我们可以将 $ 放在整个字符串的前面,它将处理整个字符串中的所有转义序列。

s/\r//' find . -name '*-e' -delete

BSD grep 将会有单独正确解释 '\r' ,但使用 $'\r' 不会有什么坏处。

BSD sed 本身会误解 's/\r//',但使用 $'s/\r//' ,我避开了那个陷阱。

请注意,我们可以将 $ 放在整个字符串的前面,它将处理整个字符串中的所有转义序列。

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.

For example, I removed all MS-DOS carriage returns from all the source files in lib/ and include/ by writing:

grep -lr 

BSD grep would have interpreted '\r' correctly on its own, but using $'\r' doesn't hurt.

BSD sed would have misinterpreted 's/\r//' on its own, but by using $'s/\r//', I avoided that trap.

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 
\r' lib include | xargs sed -i -e 

BSD grep would have interpreted '\r' correctly on its own, but using $'\r' doesn't hurt.

BSD sed would have misinterpreted 's/\r//' on its own, but by using $'s/\r//', I avoided that trap.

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.


s/\r//'
find . -name '*-e' -delete

BSD grep would have interpreted '\r' correctly on its own, but using $'\r' doesn't hurt.

BSD sed would have misinterpreted 's/\r//' on its own, but by using $'s/\r//', I avoided that trap.

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.


hello\b\\world'
hell\world
\r' lib include | xargs sed -i -e

BSD grep would have interpreted '\r' correctly on its own, but using $'\r' doesn't hurt.

BSD sed would have misinterpreted 's/\r//' on its own, but by using $'s/\r//', I avoided that trap.

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.

s/\r//' find . -name '*-e' -delete

BSD grep would have interpreted '\r' correctly on its own, but using $'\r' doesn't hurt.

BSD sed would have misinterpreted 's/\r//' on its own, but by using $'s/\r//', I avoided that trap.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文