sed join lines together

后端 未结 5 1596
面向向阳花
面向向阳花 2020-11-28 09:14

what would be the sed (or other tool) command to join lines together in a file that do not end w/ the character \'0\'?

I\'ll have lines like this

5条回答
  •  萌比男神i
    2020-11-28 10:07

    A typically cryptic Perl one-liner:

    perl -pe 'BEGIN{$/="0\n"}s/\n//g;$_.=$/'
    

    This uses the sequence "0\n" as the record separator (by your question, I'm assuming that every line should end with a zero). Any record then should not have internal newlines, so those are removed, then print the line, appending the 0 and newline that were removed.

    Another take to your question would be to ensure each line has 17 pipe-separated fields. This does not assume that the 17th field value must be zero.

    awk -F \| '
        NF == 17 {print; next}
        prev {print prev $0; prev = ""}
        {prev = $0}
    '
    

提交回复
热议问题