In a text document I want to concatenate every other line with the next. I guess sed is the thing to use? How would this be done?
在文本文档中,我希望将每个其他行与下一行连接起来。我猜sed是用的东西?这怎么办?
5 个解决方案
#1
22
This is easiest using paste
:
这是最简单的使用粘贴:
paste -s -d' \n' input.txt
Although there's a Famous Sed One-Liner (38) to emulate this as in potong's answer.
虽然有一个着名的Sed One-Liner(38)可以仿效Potong的答案。
#2
19
Unless you're really insistent that it need be sed, just pipe it through
除非你真的坚持它需要sed,否则只需通过它
paste -d" " - -
粘贴-d“” - -
#3
11
This might work for you:
这可能对你有用:
seq 10 | sed '$!N;s/\n/ /'
1 2
3 4
5 6
7 8
9 10
If is not the last line, append the following line to current line and replace the newline by a space.
如果不是最后一行,请将以下行附加到当前行,并用空格替换换行符。
#4
2
Simple awk
solution:
awk '{getline b;printf("%s %s\n",$0,b)}' file
Test:
[jaypal:~/Temp] seq 11 > file
[jaypal:~/Temp] awk '{getline b;printf("%s %s\n",$0,b)}' file
1 2
3 4
5 6
7 8
9 10
11
#5
1
What do you mean by "in a text document"? If you are editing the file with vim, you can do:
“在文本文档中”是什么意思?如果您使用vim编辑文件,则可以执行以下操作:
:g/./normal J
#1
22
This is easiest using paste
:
这是最简单的使用粘贴:
paste -s -d' \n' input.txt
Although there's a Famous Sed One-Liner (38) to emulate this as in potong's answer.
虽然有一个着名的Sed One-Liner(38)可以仿效Potong的答案。
#2
19
Unless you're really insistent that it need be sed, just pipe it through
除非你真的坚持它需要sed,否则只需通过它
paste -d" " - -
粘贴-d“” - -
#3
11
This might work for you:
这可能对你有用:
seq 10 | sed '$!N;s/\n/ /'
1 2
3 4
5 6
7 8
9 10
If is not the last line, append the following line to current line and replace the newline by a space.
如果不是最后一行,请将以下行附加到当前行,并用空格替换换行符。
#4
2
Simple awk
solution:
awk '{getline b;printf("%s %s\n",$0,b)}' file
Test:
[jaypal:~/Temp] seq 11 > file
[jaypal:~/Temp] awk '{getline b;printf("%s %s\n",$0,b)}' file
1 2
3 4
5 6
7 8
9 10
11
#5
1
What do you mean by "in a text document"? If you are editing the file with vim, you can do:
“在文本文档中”是什么意思?如果您使用vim编辑文件,则可以执行以下操作:
:g/./normal J