​A selection of topics on IT and its application to finance. 
​Send me your comments, questions ​or suggestions by clicking


Emulate Unix Join command using AWK

Lets say we have two files as shown below:

1.txt 123,4,5,6,7

2.txt 123,X Y Z,FRED,HARRY

Want to join 1.txt with 2.txt on field number 1 ( 123 ) and print out field number 2 ( X Y Z ) of file 2.txt then all fields from 1.txt

Eg resultant file to be  X Y Z,123,4,5,6,7

Here is a useful AWK command that will do just that

awk -F, "BEGIN {OFS="","" } NR==FNR {h[$1] = $2; next} {print h[$1],$1,$2,$3,$4,$5}" 2.txt 1.txt