awk : Want to print columns of different input files in line wise

Hi,

I want to print last column of 3 input files with awk.

cat file1.txt file2.txt file3.txt
file1.txt

1 ad 200
2 ss 300

file2.txt

1 mm 444
2 ee 555

file3.txt

1 kk 999
2 jj 555

My o/p should be :-

1 200 444 999
2 300 555 555

I know how to do it by using paste command can someone post how to do it by using awk.

Thanks in advance,
Satish.

In a two step process:

$ join -o 1.1 1.3 2.3 file1 file2 > file4
$ join -o 1.1 1.2 1.3 2.3 file4 file3
1 200 444 999
2 300 555 555

with awk:

awk '{a[FNR]=a[FNR]" "$3}END{for (i in a) print i a}' file1 file2 file3
1 Like

@Subbeh
Much more elegant awk then what I come up with....

awk '{a[NR]=$3} END {printf "1 ";for(i=1;i<=5;i+=2) printf "%s ",a;printf "\n2 ";for(i=2;i<=6;i+=2) printf "%s ",a;print ""}' file1 file2 file3