0

I am trying to cat the last 2 columns of multiple text files side by side. The files are in a directory of various types of files. All files have >2 columns, but no guarantee all files have the same number of columns.

For example, if I have:

file1.txt

1    a    b    J    H
2    b    c    E    E
3    c    d    L    L
4    d    e    L    L
5    e    f    O    O

file2.txt

1    a    b    M    B
2    b    c    O    E
3    c    d    O    E

I want:

J    H    M    B
E    E    O    E
L    L    O    E
L    L
O    O

The closest I've got is:

awk '{print $(NF-1), "\t", $NF}' *.txt

Which is almost what I want.

For the concatenation, I was thinking something like here for concatenation

 pr -m -t one.txt two.txt
Community
  • 1
  • 1
Lilo
  • 3
  • 1

2 Answers2

0
join -a1 -a2 one.txt two.txt | cut -d' ' -f4,5,8,9
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0
awk 'NR==FNR{a[NR]=$(NF-1)" "$NF;next}{print $(NF-1),$NF,a[FNR]}' file2.txt file1.txt

Tested:

> cat temp2
1    a    b    M    B
2    b    c    O    E
3    c    d    O    E
> cat temp1
1    a    b    J    H
2    b    c    E    E
3    c    d    L    L
4    d    e    L    L
5    e    f    O    O
> awk 'NR==FNR{a[NR]=$(NF-1)" "$NF;next}{print $(NF-1),$NF,a[FNR]}' temp2 temp1
J H M B
E E O E
L L O E
L L 
O O 
> 
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • It doesn't seem to work if the order of files is reversed. Does that have to do with the length of the first columns? – Lilo Oct 08 '14 at 06:32