Here is a method that uses paste. First to double-space the second file, so that interesting lines are in parallel. Second to paste those lines together using a value \0 or NUL, which essentially does not appear as whitespace. We can use a number of schemes to double-space the output, but paste is convenient (such as sed as noted, *for others see How can I double the newlines in an output stream ).
The display of the two original files suggests the alignment that double-spacing can accomplish. The paste of the two files using the default separator simply shows that alignment. The real answer is presented in two ways, a standard way using a temporary file, the second using process substitution.
Here is the script snippet:
FILE1=${1-data1} shift FILE2=${1-data2} E="expected-output" # Utility functions: print-as-echo, print-line-with-visual-space. pe() { for _i;do printf "%s" "$_i";done; printf "\n"; } pl() { pe;pe "-----" ;pe "$*"; } pl " Input $FILE1 and $FILE2, columnized to save visual space:" paste $FILE1 $FILE2 | expand -30 pl " Expected output:" cat $E rm -f t0 pl " Results, paste with double-space $FILE2, default options:" # sed '/^$/d;G' $FILE2 > t0 paste -d '\n' - /dev/null < $FILE2 > t0 paste $FILE1 t0 pl " Results with paste of NUL, \0:" paste -d'\0' $FILE1 t0 pl " Results with paste, process substitution:" paste -d'\0' $FILE1 <( sed '/^$/d;G' $FILE2 )
producing:
Input data1 and data2, columnized to save visual space: # yellow stars white # green twinkle red # blue on # the # sky ----- Expected output: #yellow stars #white twinkle #green on #red the #blue sky ----- Results, paste with double-space data2, default options: # yellow stars # white twinkle # green on # red the # blue sky ----- Results with paste of NUL, \0: #yellow stars #white twinkle #green on #red the #blue sky ----- Results with paste, process substitution: #yellow stars #white twinkle #green on #red the #blue sky
THis was done on a system like:
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64 Distribution : Debian 8.11 (jessie) bash GNU bash 4.3.30 paste (GNU coreutils) 8.23
cheers, drl