如何用制表符替换换行符?

发布于 2024-08-09 00:05:32 字数 232 浏览 2 评论 0原文

我有如下所示的模式,

hi
hello
hallo
greetings
salutations
no more hello for you

我尝试使用以下命令用制表符替换所有换行符,

sed -e "s_/\n_/\t_g"

但它不起作用。

有人可以帮忙吗?我正在 sed/awk 中寻找解决方案。

I have pattern like below

hi
hello
hallo
greetings
salutations
no more hello for you

I am trying to replace all newlines with tab using the following command

sed -e "s_/\n_/\t_g"

but it's not working.

Could anybody please help? I'm looking for a solution in sed/awk.

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

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

发布评论

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

评论(8

神经暖 2024-08-16 00:05:32

我认为 tr 这里更好:

tr "\n" "\t" < newlines 

正如 Nifle 在评论中建议的那样,newlines 这里是保存原始文本的文件的名称。

由于 sed 是面向行的,因此在这种情况下使用它会更加复杂。

tr is better here, I think:

tr "\n" "\t" < newlines 

As Nifle suggested in a comment, newlines here is the name of the file holding the original text.

Because sed is so line-oriented, it's more complicated to use in a case like this.

我也只是我 2024-08-16 00:05:32

不确定你想要的输出

# awk -vRS="\n" -vORS="\t" '1' file
hi      hello   hallo   greetings       salutations     no more hello for you 

not sure about output you want

# awk -vRS="\n" -vORS="\t" '1' file
hi      hello   hallo   greetings       salutations     no more hello for you 
若言繁花未落 2024-08-16 00:05:32
sed '$!{:a;N;s/\n/\t/;ta}' file
sed '$!{:a;N;s/\n/\t/;ta}' file
草莓酥 2024-08-16 00:05:32
paste -s

-s 连接每个单独输入文件的所有行
命令行命令。每行的换行符
除了每个输入文件中的最后一行被替换为
制表符,除非 -d 选项另有指定。

paste -s

-s Concatenate all of the lines of each separate input file in
command line order. The newline character of every line
except the last line in each input file is replaced with the
tab character, unless otherwise specified by the -d option.

过度放纵 2024-08-16 00:05:32

您无法使用 sed 逐行替换换行符。您必须累积行并替换它们之间的换行符。

text abc\n    <- can't replace this one

text abc\ntext def\n    <- you can replace the one after "abc" but not the one at the end

这个 sed 脚本会累积所有行并消除除最后

sed -n '1{x;d};${H;x;s/\n/\t/g;p};{H}'

一行之外的所有换行符:顺便说一句,您的 sed 脚本 sed -e "s_/\n_/\ t_g" 试图说“将所有斜杠后跟换行符替换为斜杠后跟制表符”。下划线承担了 s 命令的分隔符作用,以便斜杠可以更轻松地用作搜索和替换的字符。

You can't replace newlines on a line-by-line basis with sed. You have to accumulate lines and replace the newlines between them.

text abc\n    <- can't replace this one

text abc\ntext def\n    <- you can replace the one after "abc" but not the one at the end

This sed script accumulates all the lines and eliminates all the newlines but the last:

sed -n '1{x;d};${H;x;s/\n/\t/g;p};{H}'

By the way, your sed script sed -e "s_/\n_/\t_g" is trying to say "replace all slashes followed by newlines with slashes followed by tabs". The underscores are taking on the role of delimiters for the s command so that slashes can be more easily used as characters for searching and replacing.

败给现实 2024-08-16 00:05:32

另外,如果使用变量、此处字符串或此处文档:

tr "\n" "\t" <<< "a
b
c
d
"
paste -s <<EOF
a
b
c
d
EOF

Also if using Variables, Here Strings or Here Documents:

tr "\n" "\t" <<< "a
b
c
d
"
paste -s <<EOF
a
b
c
d
EOF
羁客 2024-08-16 00:05:32

与 HackerRank 上的文本处理挑战类似,这里是解决方案以及描述:

paste -sd'\t' data.txt
  • -s:并排列出行。

  • -d'\t':将分隔符设置为制表符。


该命令的简短版本是:

paste -s data.txt

请注意,使用 -s 选项时,tab 是默认分隔符。

Similar to the Text Processing challenge on HackerRank, here is the solution along with the description:

paste -sd'\t' data.txt
  • -s: lists the lines side by side.

  • -d'\t': sets the delimiter to a tab.


The short version of the command is:

paste -s data.txt

Note that tab is the default delimiter when using the -s option.

等数载,海棠开 2024-08-16 00:05:32

您的 sed 脚本就快到了,您只需将其更改为:

sed -e "s/\n/\t/g"

\ 就足以转义,您不需要添加 _
并且您需要在末尾的 g 之前添加 / 让 sed 知道这是脚本的最后一部分。

You are almost there with your sed script, you'd just need to change it to:

sed -e "s/\n/\t/g"

The \ is enough for escape, you don't need to add _
And you need to add the / before g at the end to let sed know that this is the last part of the script.

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