[NTLUG:Discuss] Is there an easy way?

David Stanaway david at stanaway.net
Sun Jul 26 08:56:20 CDT 2009


AWK is you friend write year, or perl or python or ruby.

AWK carries less baggage.

If you can come up with a pattern which matches lines from one of the 
files and one of the files only.

awk script:

merge.awk -----------------------------------------------------
$0 =~ /,[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ { phonemap[$1] = $2 };
$0 =~ /[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*,/ { ipmap[$1] = $2 };
$0 =~ /,[a-zA-Z0-9]*\/[0-9]*/ { macmap[$1] = $2 };
END {
   for ( phone in phonemap ) {
     ip = phonemap[phone];
     mac = ipmap[ip];
     port = macmap[mac];
     printf("%s,%s\n",phone,port);
  }
}
-----------------------------------------------------------------


awk -F, -f merge.awk  file1.txt file2.txt file3.txt



Leroy Tennison wrote:
> I'm not even sure how to Google for this one without having to wade 
> through too many extraneous hits so I'm posting here.  I have three 
> files and need to merge fields from each of them into a single file.  A 
> brief description of each is:
>
> File 1 has a phone number and an associated IP address (VOIP, Avaya PBX)
> File 2 has an IP address and an associated MAC address (Cisco router).
> File 3 has a MAC address and associated Cisco switch port assignment.
>
> I need a file containing phone number and associated switch port.
>
> This may be somewhat OT except I'm hoping there's a Linux utility which 
> will meet the need.  I know that I can turn each file into a database 
> table and do some operation on two tables to "merge" their fields and 
> then repeat again to get what I need but that's the extent of my 
> understanding of that option.  Wondering if there's a utility to take 
> the files directly and do the "merge".  This is a situation where 
> scripting seems to be the wrong solution, I've written something similar 
> in awk and don't want to do that again.  I'm also not looking forward to 
> writing a program in a "real programming language" to accomplish this 
> either.  Thanks for any input.
>
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
>
>   




More information about the Discuss mailing list