[NTLUG:Discuss] Is there an easy way?

Robert Citek robert.citek at gmail.com
Sun Jul 26 18:13:05 CDT 2009


On Sun, Jul 26, 2009 at 8:08 AM, Patrick R. Michaud<pmichaud at pobox.com> wrote:
> On Sun, Jul 26, 2009 at 12:06:14AM -0500, Leroy Tennison wrote:
>> I need a file containing phone number and associated switch port.
>> [...]  Wondering if there's a utility to take
>> the files directly and do the "merge".
>
> You might want to look at the "join" command then  ("man join").
> Here's an example:

Nice example.  join is wonderful and fast.  If you want to do more
sophisticated data manipulation, you can easily switch to using
sqlite.  Using the same dataset (assuming it's tab-delimited), here's
how:

$ cat switch.sql
create table file1 (phone, ip);
create table file2 (ip, mac);
create table file3 (mac, port);

create index f1_ip on file1 (ip) ;
create index f2_ip on file2 (ip) ;
create index f2_mac on file2 (mac) ;
create index f3_mac on file3 (mac) ;

.mode tabs
.imp "file1" "file1" ;
.imp "file2" "file2" ;
.imp "file3" "file3" ;

select file1.phone, file3.port
 from file1 join file2 on file1.ip=file2.ip
 join file3 on file2.mac=file3.mac
 order by file1.phone, file3.port ;

$ sqlite3 switch.db < switch.sql
555-0345	port23
555-1212	port16
555-1323	port08
555-1458	port05
555-2143	port01

Regards,
- Robert



More information about the Discuss mailing list