Side-by-side comparison of more than two files containing numerical values

awkdiff()text processing

I have three files containing a sorted sequence of numbers, one per line :

file1

1
2
3

file2

1
3
4

file3

1
5

I want to "align" these three files side-by-side like the following :

file1  file2  file3
1      1      1
2      
3      3
       4
              5

I've tried with sdiff but it only works with 2 files

Best Answer

You could process each file and print a line with some character e.g. X for every missing number in the sequence 1-max (where max is the last number in that file), paste the results then replace that character with space:

paste \
<(awk 'BEGIN{n=1};{while (n<$1) {print "X";n++}};{n=$1+1};1' file1) \
<(awk 'BEGIN{n=1};{while (n<$1) {print "X";n++}};{n=$1+1};1' file2) \
<(awk 'BEGIN{n=1};{while (n<$1) {print "X";n++}};{n=$1+1};1' file3) \
| tr X ' '

If a certain value is missing from all files you'll get empty lines in your output (actually they're not empty, they contain only blanks).
To remove them replace tr X ' ' with sed '/[[:digit:]]/!d;s/X/ /g' Also, if you need a header you can always run something like this first:

 printf '\t%s' file1 file2 file3 | cut -c2-
Related Question