Wednesday, March 4, 2009

AWK Magic

Suppose you have the two files with following content

ip.txt

bmdes001;172.16.33.100
bmdes002;172.16.33.101
bmdes003;172.16.33.102
bmdes004;172.16.33.103
bmdes005;172.16.33.104

dhcp.txt

00:1A:4D:A6:B5:4C;bmdes001;
00:1D:09:25:7C:B2;bmdes002;
00:1D:09:19:82:EF;bmdes003;
00:1D:09:26:45:46;bmdes004;
00:1D:09:19:62:79;bmdes005;

And you want to combine the two files where 1st column of ip.txt file matches with 2nd column of dhcp.txt then following is the command for you.

awk 'BEGIN{FS=";"}FILENAME ~ /ip.txt/{a[$1]=$2}FILENAME ~ /dhcp.txt/{print $1,$2,a[$2]}' ip.txt dhcp.txt

00:1A:4D:A6:B5:4C bmdes001 172.16.33.100
00:1D:09:25:7C:B2 bmdes002 172.16.33.101
00:1D:09:19:82:EF bmdes003 172.16.33.102
00:1D:09:26:45:46 bmdes004 172.16.33.103
00:1D:09:19:62:79 bmdes005 172.16.33.104

No comments:

Post a Comment