Joining two consecutive lines using awk or sed

前端 未结 7 1993
一生所求
一生所求 2020-12-14 17:54

How would I join two lines using awk or sed?

for e.g.:

I have data that looks like this:

abcd
joinabcd
efgh
joinefgh
ijkl
j         


        
相关标签:
7条回答
  • 2020-12-14 18:15

    They say imitation is the sincerest form of flattery.
    Here's a Perl solution inspired by Dimitre's awk code:

    perl -lne 'print "$_$p" if $. % 2 == 0; $p = $_' infile
    

    $_ is the current line
    $. is the line number

    0 讨论(0)
  • 2020-12-14 18:16

    You can use printf with a ternary:

        awk '{printf (NR%2==0) ? $0 "\n" : $0}'
    
    0 讨论(0)
  • 2020-12-14 18:18

    This is the answer to the question "How to make the count and the file appear on the same line" in the command:

    find . -type f -exec fgrep -ci "MySQL" {} \; -print
    

    Bryon Nicolson's answer produced the best result.

    0 讨论(0)
  • 2020-12-14 18:21

    Some improvement to the "sed" script above that will take the following: 1008
    -2734406.132904 2846
    -2734414.838455 4636
    -2734413.594009 6456
    -2734417.316269 8276
    -2734414.779617

      and make it :    
    

    1008 -2734406.132904
    2846 -2734414.838455
    4636 -2734413.594009
    6456 -2734417.316269
    8276 -2734414.779617

      the "sed" is : "sed 'h;s/.*//;G;N;s/\n/ /g'"
    
    0 讨论(0)
  • 2020-12-14 18:30
    awk 'BEGIN{i=1}{line[i++]=$0}END{j=1; while (j<i) {print line[j+1] line[j]; j+=2}}' yourfile
    

    No need for sed.

    0 讨论(0)
  • 2020-12-14 18:40
    awk '!(NR%2){print$0p}{p=$0}' infile
    
    0 讨论(0)
提交回复
热议问题