Skip site navigation (1)Skip section navigation (2)
Date:      07 Feb 2001 13:27:02 -0600
From:      Tim Ayers <tayers@bridge.com>
To:        freebsd-questions@FreeBSD.ORG
Subject:   Re: sed & awk help...?!
Message-ID:  <d7cu1egp.fsf@tim.bridge.com>
In-Reply-To: Marcus Ramos's message of "Wed, 07 Feb 2001 16:29:38 -0200"
References:  <054F7DAA9E54D311AD090008C74CE9BD01F1E94E@exchange.panasonicfa.com> <3A819412.BF10DCB2@ansp.br>

next in thread | previous in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?d7cu1egp.fsf>