sed join lines together

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

412|n|Leader Building Material||||||||||d|d|20||0

which need to be left alone, and then I'll have lines like this for example (which is 3 lines, but only one ends w/ 0)

107|n|Knot Tying Tools|||||Knot Tying Tools

|||||d|d|0||0

which need to be joined/combined into one line

107|n|Knot Tying Tools|||||Knot Tying Tools|||||d|d|0||0

Solution 1:

 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 

Solution 2:

awk could be short too:

awk '!/0$/{printf $0}/0$/'

test:

kent$  cat t
#aasdfasdf
#asbbb0
#asf
#asdf0
#xxxxxx
#bar

kent$  awk '!/0$/{printf $0}/0$/' t
#aasdfasdf#asbbb0
#asf#asdf0
#xxxxxx#bar 

Solution 3:

The rating of this answer is surprising ;s (this surprised wink emoticon pun on sed substitution is intentional) given the OP specifications: sed join lines together.

This submission's last comment

"if that's the case check what @ninjalj submitted"

also suggests checking the same answer.

ie. Check using sed ':a;/0$/{N;s/\n//;ba}' verbatim

sed ':a;/0$/{N;s/\n//;ba}'
 does
 no one
 ie. 0
 people,
 try
 nothing,

 ie. 0
 things,
 any more,
 ie. 0
 tests?

          (^D aka eot 004 ctrl-D ␄  ... bash generate via: echo ^V^D)

which will not give (do the test ;):

 does no one ie. 0
 people, try nothing, ie. 0
 things, any more, ie. 0
 tests?          (^D aka eot 004 ctrl-D ␄  ... bash generate via: echo ^V^D)

To get this use:

sed 'H;${z;x;s/\n//g;p;};/0$/!d;z;x;s/\n//g;'

or:

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

not:

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

Notes:

sed 'H;${x;s/\n//g;p;};/0$/!d;z;x;s/\n//g;'

does not use branching and
is identical to:

sed '${H;z;x;s/\n//g;p;};/0$/!{H;d;};/0$/{H;z;x;s/\n//g;}'
  • H commences all sequences
  • d short circuits further script command execution on the current line and starts the next cycle so address selectors following /0$/! can only be /0$/!! so the address selector of
    /0$/{H;z;x;s/\n//g;} is redundant and not needed.
  • if a line does not end with 0 save it in hold space
    /0$/!{H;d;}
  • if a line does end with 0 save it too and then print flush (double entendre ie. purged and lines aligned)
    /0$/{H;z;x;s/\n//g;}
  • NB ${H;z;x;s/\n//g;p;} uses /0$/ ... commands with an extra p to coerce the final print and with a now unnecessary z (to empty and reset pattern space like s/.*//)

Solution 4:

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}
'