Concatenating every other line with the next

后端 未结 5 1194
醉话见心
醉话见心 2020-12-14 15:11

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?

相关标签:
5条回答
  • 2020-12-14 15:34

    Unless you're really insistent that it need be sed, just pipe it through

    paste -d" " - -

    0 讨论(0)
  • 2020-12-14 15:36

    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.

    0 讨论(0)
  • 2020-12-14 15:40

    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 
    
    0 讨论(0)
  • 2020-12-14 15:43

    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.

    0 讨论(0)
  • 2020-12-14 15:56

    What do you mean by "in a text document"? If you are editing the file with vim, you can do:

    :g/./normal J
    
    0 讨论(0)
提交回复
热议问题