From owner-freebsd-questions Wed Feb 7 11:28: 1 2001 Delivered-To: freebsd-questions@freebsd.org Received: from bcfw1d.bridge.com (bcfw1d.ext.bridge.com [167.76.159.31]) by hub.freebsd.org (Postfix) with ESMTP id 3A47F37B491 for ; Wed, 7 Feb 2001 11:27:37 -0800 (PST) Received: (from uucp@localhost) by bcfw1d.bridge.com (8.10.2/8.10.2) id f17JSZE03440; Wed, 7 Feb 2001 13:28:36 -0600 (CST) Received: from unknown(167.76.56.34) by bcfw1d.bridge.com via smap (V5.5) id xma003354; Wed, 7 Feb 01 13:28:17 -0600 Received: from mnmailhost (mnmailhost.bridge.com [167.76.155.14]) by mail1srv.bridge.com (8.8.8/8.7.3) with SMTP id NAA24735; Wed, 7 Feb 2001 13:27:13 -0600 (CST) Received: from 89-7 by mnmailhost (SMI-8.6/SMI-4.1) id OAA27351; Wed, 7 Feb 2001 14:27:04 -0500 To: freebsd-questions@FreeBSD.ORG Subject: Re: sed & awk help...?! References: <054F7DAA9E54D311AD090008C74CE9BD01F1E94E@exchange.panasonicfa.com> <3A819412.BF10DCB2@ansp.br> From: Tim Ayers Date: 07 Feb 2001 13:27:02 -0600 In-Reply-To: Marcus Ramos's message of "Wed, 07 Feb 2001 16:29:38 -0200" Message-ID: Lines: 66 User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.5 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG I didn't see the original post, but here is a Perl solution. Actually two: the first is a script-quality solution and the second is the one-liner throw-away solution. Both read from STDIN, so just pipe your data or include the filename as an argument, e.g. cat mydata.txt | solution1.pl or perl -lane'push@a,$F[0];$b{$F[1]}=$F[2];END{$b{$_}&&print"$_ $b{$_}"for(sort@a)}' mydata.txt -------------------------- solution 1 --------------------------------- #!/usr/bin/perl -w use strict; my @keys = (); my %mapping = (); while (<>) { chop; my ($a, $b, $c) = split /\s+/; push @keys, $a; $mapping{$b} = $c; } for (sort @keys) { print "$_ => $mapping{$_}\n" if exists $mapping{$_}; } __END__ -------------------------- solution 2 ---------------------------------- perl -lane'push@a,$F[0];$b{$F[1]}=$F[2];END{$b{$_}&&print"$_ $b{$_}"for(sort@a)}' __END__ HTH and Hope you have a very nice day, :-) tim M> "Zaitsau, Andrei" wrote: >> Hello Everyone, >> I have small work related problem. >> Can someone help me with that? It's more UNIX question. >> >> Please CC me as I am not subscribed to a list. >> >> Here The Problem. >> I have a file. And there is 3 tables in it. >> >> 1 12 ac >> 3 4 ab >> 5 6 ac >> 7 8 ac >> 9 1 ab >> 2 13 ab >> 4 5 ac >> 6 7 ab >> 8 9 ab >> >> The thing I need is to do some sorting. >> I guess it could be done with 'awk' or 'sed' utilities. >> >> Okay program looking at the first value of the first table and compare it to >> the value in the second table. If it finds a match on the second table it >> outputs to other file matching value plus corresponding value from the 3rd >> table (just look at it as 2nd and 3rd tables are hard linked). >> To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message