I have three files and I have to do something like this:-
File A
1232|||1111 0001|||
1232|||2222 0001|||
1232|||4444 0001|||
1232|||4444 0001|||
File B
1232|1111 0001|||002222||
1232|2222 0001|||003333||
1232|3333 0001|||004444||
File C
1232|002222|||
1232|005555|||
Files are pipe delimited
For every line in File A, If Column 4 in File A is missing in all the rows in File B in Column 2, we print miss1
If it is present in any one row of File B, we check, if Col 5 of file B is present in any row of file C in Col 2.
If it is, we print "ok", else print "miss2".
I needed one liner for awk, so that I could run it from command prompt.
I tried this. It works, but it does not print correct message:-
nawk 'BEGIN{ FS="|";c=0;d=0} NR==FNR{f1[$2]=$5;c++;next} NR==FNR+c {f2[$2]=$0;d++} NR==FNR+c+d{ e=f1[$4]; print $4 in f1?$4 ( e in f2?" ok ":" miss2 "):$4" miss1 " }END {}' B C A
open FH,"<a.txt";
while(<FH>){
my @tmp=split("[|]",$_);
push @arr,$tmp[3];
}
close FH;
open FH,"<b.txt";
while(<FH>){
my @tmp=split("[|]",$_);
$hash1{$tmp[1]}=$tmp[4];
}
close FH;
open FH,"<c.txt";
while(<FH>){
my @tmp=split("[|]",$_);
$hash2{$tmp[1]}=1;
}
close FH;
for($i=0;$i<=$#arr;$i++){
if (not exists $hash1{$arr[$i]}){
print $arr[$i]," miss1\n";
next;
}
else{
if (not exists $hash2{$hash1{$arr[$i]}}){
print $arr[$i]," miss2\n";
next;
}
else{
print $arr[$i]," ok\n";
}
}
}
but, if you could just also tell me why this awk code is showing wrong message, that will help me further.
nawk 'BEGIN{ FS="|";c=0;d=0} NR==FNR{f1[$2]=$5;c++;next} NR==FNR+c {f2[$2]=$0;d++} NR==FNR+c+d{ e=f1[$4]; print ,( $4 in
f1?$4 ,( e in f2?" ok ":e" misses2 ") ) :$4" misses1 " }END {}' B C A
When $4 is not present in f1 , should this not directly print $4 misses1 rather than printing misses2.?
nawk 'BEGIN{ FS="|";c=0;d=0} NR==FNR{f1[$2]=$5;c++;next} NR==FNR+c {f2[$2]=$0;d++} NR==FNR+c+d{ print $4 in f1?$4 ( f1[$4] in f2?" ok ":" miss2 "):$4" miss1 " }END {}' B C A