For something that can take any number of groups separated by blank lines in both files, your could try:
awk '
function print_rest() {
while(c1[g2] > c2)
printf("%s\t\n", d[g2, ++c2])
if(++g2 <= g1)
print ""
c2 = 0
}
BEGIN { g1 = g2 = 1
}
FNR == NR {
if(!NF) {
g1++
next
}
d[g1, ++c1[g1]] = $1
next
}
!NF { print_rest()
next
}
{ printf("%s\t%s\n", d[g2, ++c2], $1)
}
END { print_rest()
}' file1 file2
If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .
If you want to keep the spaces that were present in some of your sample file1 and file2 input lines, change both occurrences of $1 in the above script with $0 . (Using $1 seemed to produce output closer to what you said you wanted.)
When run with your sample input files, the above produces the output:
Did you try changing $1 in both places in my script to $0 as I suggested in my post with that script if you wanted to preserve spaces in your input files?
If that didn't work, please show us the output my script produced after that change (in CODE tags) and clearly specify what needs to be done differently.
Indeed the output was not exact, and one line was even left out.
Here is another fix:
while
read line2 <&4
e2=$?
read line1 <&3 || [ $e2 -eq 0 ]
do
if [ -z "$line1" ] && [ -n "$line2" ]
then
while
printf "\t%s\n" "$line2"
read line2 <&4 && [ -n "$line2" ]
do :
done
elif [ -z "$line2" ] && [ -n "$line1" ]
then
while
printf "%s\n" "$line1"
read line1 <&3 && [ -n "$line1" ]
do :
done
fi
printf "%s\t%s\n" "$line1" "$line2"
done 3<file1 4<file2
Here the printf is moved to the while, again using the "list" feature of while (takes the exit status of the last command).
An empty loop causes an error, therefore the : .
In effect it emulates a repeat-until (or do-until) loop.