How to combine column from multiple text files? [duplicate] How to combine column from multiple text files? [duplicate] shell shell

How to combine column from multiple text files? [duplicate]


Try this:

paste File[0-9]*_example.txt | awk '{i=3;while($i){printf("%s ",$i);i+=3}printf("\n")}'

Example:
File1_example.txt:

A   123   1B   234   2C   345   3D   456   4

File2_example.txt:

A   123   5B   234   6C   345   7D   456   8

Run command as:

$ paste File[0-9]*_example.txt | awk '{i=3;while($i){printf("%s ",$i);i+=3}printf("\n")}'

Output:

1 5 2 6 3 7 4 8


I tested below code with first 3 files

cat File*_example.txt  | awk '{a[$1$2]= a[$1$2] $3 " "} END{for(x in a){print a[x]}}' | sort1 5 92 6 103 7 114 8 12

1) use an awk array, a[$1$2]= a[$1$2] $3 " " index is column1 and column2, array value appends all column 3.

2) END{for(x in a){print a[x]}} travesrsed array a and prints all values.

3)use sort to sort the output.


when cating you need to ensure the file order is preserved, one way is to explicitly specify the files

cat File{1..100}_example.txt | awk '{print $NF}' | pr 4ts' '

extract last column by awk and align using pr