3 files in one awk one liner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 3 files in one awk one liner
# 1  
Old 02-23-2009
3 files in one awk one liner

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


Expected O/P
1111 0001 ok
2222 0001 miss2
4444 0001 miss1
4444 0001 miss1


my o/p from awk command:-
1111 0001 ok
2222 0001 miss2
4444 0001 miss2
4444 0001 miss2

I don"t know why it is printing misses2 when it should be misses1 for 4444 0001.
any help will be appreciated.
# 2  
Old 02-24-2009
Code:
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";
		}
	}
}

# 3  
Old 02-24-2009
cherry can u tell me:

what NR is used by kishal in above post
# 4  
Old 02-24-2009
thanks cherry. that works perfectly.

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.?
# 5  
Old 03-01-2009
I have finally done that with a minor change:-

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

1111 0001 ok
2222 0001 miss2
4444 0001 miss1
4444 0001 miss1

Thanks for responses.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk one liner

The below code is a simple modified sample from a file with millions of lines containing hundreds of extra columns xxx="yyy" ... <app addr="1.2.3.4" rem="1000" type="aaa" srv="server1" usr="user1"/> <app usr="user2" srv="server2" rem="1001" type="aab" addr="1.2.3.5"/>What's the most efficient awk... (2 Replies)
Discussion started by: cabrao
2 Replies

2. Shell Programming and Scripting

Combine these two into one liner awk?

ignore the simplicity of the foo file, my actual file is much more hardcore but this should give you the jist of it. need to combine the two awks into one liner. essentially, need to return the value of one particular field in a file that has multiple comma separated fields. thanks guys cat foo... (1 Reply)
Discussion started by: jack.bauer
1 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. UNIX for Dummies Questions & Answers

renaming multiple files using sed or awk one liner

hi, I have a directory "test" under which there are 3 files a.txt,b.txt and c.txt. I need to rename those files to a.pl,b.pl and c.pl respectively. is it possible to achieve this in a sed or awk one liner? i have searched but many of them are scripts. I need to do this in a one liner. I... (2 Replies)
Discussion started by: pandeesh
2 Replies

5. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

6. Shell Programming and Scripting

Awk one-liner?

Hello, I have two files... File #1 1 3 2 5 File #2 3 5 3 1 3 7 9 1 5 2 5 8 3 3 1 I need to extract all lines from File #2 where the first two columns match each line of File #1. So in the example, the output would be: 1 3 7 2 5 8 Is there a quick one-liner that would... (4 Replies)
Discussion started by: palex
4 Replies

7. UNIX for Dummies Questions & Answers

need an awk one liner

example input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

8. Shell Programming and Scripting

grep-awk one liner help

Hi guys, I'm trying to create a one line command that does the following. I will post my command first so you can get the idea better: ls -larht | awk '{print $4}' | uniq | xargs grep * __________ ls -larht | awk '{print $4}' | uniq This will post the name of the groups of each file... (2 Replies)
Discussion started by: erick_tuk
2 Replies

9. UNIX for Dummies Questions & Answers

awk one liner

I need a one liner to" find /pattern/ print from x lines before "pattern" to y lines after "pattern" (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

10. Shell Programming and Scripting

awk one liner

input a 100 200 300 b 400 10 output a 100 a 200 a 300 b 400 b 10 Thanx (6 Replies)
Discussion started by: repinementer
6 Replies
Login or Register to Ask a Question