Bash – Trying to add a newline to the paste command

bashnewlinespaste

Here is the weak attempt at a paste command trying to include a newline:

    paste -d -s tmp1 tmp2 \n tmp3 \n tmp4 tmp5 tmp6 > tmp7

Basically I have several lines in each tmp and I want the output to read

First(tmp1) Last(tmp2)
Address(tmp3)
City(tmp4) State(tmp5) Zip(tmp6)

Am I way off base with using a newline in the paste command?

Here is my finished product: THANK YOU FOR THE HELP!

    cp phbook phbookh2p5

    sed 's/\t/,/g' phbookh2p5 > tmp
    sort -k2 -t ',' -d tmp > tmp0
    cut -d',' -f1,2 tmp0 > tmp1
    cut -d',' -f3 tmp0 > tmp2
    cut -d',' -f4,5,6 tmp0 > tmp3
    echo "" > tmp4

    paste -d '\n' tmp1 tmp2 tmp3 tmp4 > tmp7

    sed 's/\t/ /g' tmp7 > phbookh2p5

    cat phbookh2p5

    rm tmp*; rm phbookh2p5

Best Answer

Try this solution with two extra temporary files:

paste tmp1 tmp2 > tmp12
paste tmp4 tmp5 tmp6 > tmp456
paste -d "\n" tmp12 tmp3 tmp456 > tmp7

This solution was based on the assumption that the -d option selects the delimiter globally for all input files so it either be a blank or a newline. In a way this is true since later occurences of -d overwrite previous ones. However, as @DigitalTrauma pointed out we can supply more than one delimiter which will be used sequentially. So @DigitalTrauma's solution is more elegant than mine since it completely avoids additional temporary files.

One niche application for my solution would be the case in which one or delimiters with more than one character each have to be used. This should not be possible with just using the -d option.

Related Question