user1606106
user1606106

Reputation: 207

extract multiple columns from multiple files with bash, awk or sed

I am trying to extract column1 and column7 from the text files of two different directories. The names of files are same in these directories. I need to save the outputs to another directory.

file1.txt in D1 directory

column1  column2  column3  column7
david    45.2       14.6   45.0
grecy    12.0       0.0    12.0
anjal    23.2       321.0  34.5

file1.txt in D2 directory

column1  column2  column3  column7
david    45.2       14.6   56.0
grecy    10.0       0.0    12.0
moha     23.1       321.0  334.5

output format (save the output as file1.txt in D3 directory)

column1  column1  column7  column7
david      david    45.0   56.0
grecy      grecy    12.0   12.0
anjal      moha     34.5   334.5

Your suggestions would be appreciated.

Upvotes: 1

Views: 5208

Answers (3)

Steve
Steve

Reputation: 54392

A quick way using paste and awk:

paste D1/file1.txt D2/file1.txt | awk '{ printf "%s\t%s\t%s\t%s\n", $1, $5, $4, $8 }' > D3/file1.txt

Results:

column1 column1 column7 column7
david   david   45.0    56.0
grecy   grecy   12.0    12.0
anjal   moha    34.5    334.5

EDIT: To process multiple files, you can use a bash loop:

for i in D1/*.txt; do paste "$i" D2/${i/D1\//} | awk '{ printf "%s\t%s\t%s\t%s\n", $1, $5, $4, $8 }' > D3/${i/D1\//}; done

Upvotes: 3

Vaughn Cato
Vaughn Cato

Reputation: 64308

awk '{
     a1=$1;b1=$4;
     getline <"D2/file1.txt";
     print a1,$1,b1,$4
}' D1/file1.txt >D3/file1.txt

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 360095

join -j 1 -o 1.1,2.1,1.7,2.7 <(D1/file1.txt | sort -k1,1) <(sort -k1,1 D2/file1.txt) > D3/file1.txt

Upvotes: 0

Related Questions