«

»

Mar
30

Merge two files using AWK

We may use AWK to easily join two files. Assume we have input files foo and foo2

Content of foo

OSL 59.9 10.9
TRD 62.7 11.8

… and foo2

OSL 25 m
TRD 36 m

Join the two files with AWK commando

awk ‘NR==FNR{a[FNR]=$0;next} {print a[FNR],$2,$3}’ foo foo2

or

awk ‘{str=$2;str2=$3; getline < "foo";printf "%3s %3.1f %3.1f %2f %1s\n", $1,$2,$3,str,str2}' foo2

Output will be:

OSL 59.9 10.9 25 m
TRD 62.7 11.8 36 m

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


*

This blog is kept spam free by WP-SpamFree.