sed join lines together

后端 未结 5 1590
面向向阳花
面向向阳花 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条回答
  •  抹茶落季
    2020-11-28 09:47

     sed ':a;/0$/{N;s/\n//;ba}'
    

    In a loop (branch ba to label :a), if the current line ends in 0 (/0$/) append next line (N) and remove inner newline (s/\n//).

    awk:

    awk '{while(/0$/) { getline a; $0=$0 a; sub(/\n/,_) }; print}'
    

    Perl:

    perl -pe '$_.=<>,s/\n// while /0$/'
    

    bash:

    while read line; do 
        if [ ${line: -1:1} != "0" ] ; then 
            echo $line
        else echo -n $line
    fi
    done 
    

提交回复
热议问题