[NTLUG:Discuss] Is there an easy way?
Leroy Tennison
leroy_tennison at prodigy.net
Mon Jul 27 23:23:42 CDT 2009
Carl Haddick wrote:
> On Sun, Jul 26, 2009 at 12:06:14AM -0500, 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
>
> Ok, I'll admit, I keep hoping someday somebody will need a Python
> hacker. I guess I really know how to pick languages, I've also got
> extensive Forth experience. But I gotta say for some things Forth can
> really get the job done. If I was ever stranded on a desert island with
> nothing but a PDP11 running nothing but a front panel I'd have Forth
> running in a week, which would let me write a simple word processor in
> another week, and then I could print out my message to tuck in a handy
> bottle...
>
> Anyway, two 'for' loops in Python came to mind:
>
> #!/usr/local/bin/python
>
> phoneip={}
> macswitch={}
> ipmac={}
>
> for d in zip(
> (file('phoneip').readlines(),
> file('ipmac').readlines(),
> file('macswitch').readlines()),
> (phoneip,
> ipmac,
> macswitch)
> ):
> for l in d[0]:
> key,val=l.strip().rsplit(' ',1)
> d[1][key]=val
>
> for p in phoneip:
> ip=phoneip[p]
> mac=ipmac[ip]
> port=macswitch[mac]
> print 'Phone:%s has ip: %s, mac: %s, and is on port: %s'%(p,ip,mac,port)
>
> The output from that is like:
>
> Phone:(666) 234-5678 has ip: 4.5.6.7, mac: 22:33:44:55:66:77:88:99, and
> is on port: p2
> Phone:(777) 345-6789 has ip: 7.8.9.10, mac: 33:44:55:66:77:88:aa, and is
> on port: p3
> Phone:(555) 123-4567 has ip: 1.2.3.4, mac: 11:22:33:44:55:66:77:88, and
> is on port: p1
>
> But don't let me argue against the simple approach. Cut, join, uniq,
> sort, and most certainly awk are all supremely useful. However, if you
> need to pick through the files, regex out what you need, maybe do some
> other housekeeping, a script approach might be the easy way out.
>
> Three for loops. That was three for loops in my Python blatherings, not
> two.
>
> Carl
>
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
>
Thanks for the input, I found that join really did meet the need.
More information about the Discuss
mailing list