Salil, do you have a solution to this ? I am not sure why you are putting unnecessary comments on this post. A solution/help from you is much appreciated
not a very decent solution this. but give it a try.
$ cat file
x a 10
y a 10
z a 11
x b 10
y b 12
z b 10
x c 0
y c 0
x d 1
y d 1
z d 1
w d 2
x e 1
y e 1
z e 1
a,b and d are the records you want printed from "file".
printing out the required "$2"s below....
$ sort -u -k 2,3 file | nawk 'BEGIN {a=$2} {if($2==a && NF > 0){print $2}}{a=$2}'
a
b
d
and this is your solution....
$ for i in `sort -u -k 2,3 file | nawk 'BEGIN {a=$2} {if($2==a && NF > 0){print $2}}{a=$2}'`; do nawk -v var=$i '$2 == var {print $0}' file; done
x a 10
y a 10
z a 11
x b 10
y b 12
z b 10
x d 1
y d 1
z d 1
w d 2
if you dont want to call nawk for each record that meets the criteria you can print the required "$2"s to a file and fetch them into an array using getline and split.
#!/usr/bin/perl
use strict;
my (%hash,%h);
open FH,"<a";
while(<FH>){
my @tmp=split(" ",$_);
$hash{$tmp[1]}.=$_;
$h{$tmp[1]}->{$tmp[2]}=1;
}
close FH;
for my $key (sort keys %hash){
my @tmp=keys %{$h{$key}};
print $hash{$key} if $#tmp>=1;
}
yes. it really does. compact piece of code there. but, if you still want to use shell scripts here you go.
#! /bin/bash
gawk 'BEGIN{
while (getline < "file") {
twothree[$2$3]=$2;
}
for (i in twothree) {
if (twothree in two) {
filtered[twothree];}
else {
two[twothree];}
}}
{if ($2 in filtered) {print $0;}
}' file