The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
comparing files Aditya.Gurgaon Shell Programming and Scripting 4 02-05-2009 08:46 PM
comparing of two files with output which is not in both files suryanarayana Shell Programming and Scripting 2 02-04-2009 08:41 AM
Comparing two files using awk kanu_kanu Shell Programming and Scripting 2 09-16-2008 08:30 AM
Need Help Comparing two Files awknerd Shell Programming and Scripting 1 09-01-2008 03:06 AM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-09-2007 02:38 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-05-2009
kbirde kbirde is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
Comparing two files

Hello there,

I've two files to compare as follows:

File1
1001
1003
1005
1007

File2
1005 1003 1007 1001
1002 1001 1006 1004
1003 1001 1005 1005
1003 1002 1006 1004

I would like to write a new File3 only if all the fields on each row in File2 contains any of the values in File1.

Based on the above my File3 would look like:
1005 1003 1007 1001
1003 1001 1005 1005

Thanks a lot.
  #2 (permalink)  
Old 05-05-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,814

Code:
awk ' FILENAME=="file1" { arr[$0]++}
        FILENAME=="file2" { ok=1; 
                                 for(i=1; i<=NF && ok==1; i++) 
                                       { if($i in arr) {ok=1} else {ok=0} }
                                 if(ok==1) { print $0}  } ' file1 file2 > file3

  #3 (permalink)  
Old 05-05-2009
kbirde kbirde is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
Hi Jim,

This works perfect. I would like to request one modification. Instead of comparing all the fields on each row, can we modify the script to compare only the second, third and fourth field on each row of File2 with all the values in File1 and if its a match, then to write out the entire row (resulting in the same output as before).

Regards,
kbirde
  #4 (permalink)  
Old 05-05-2009
kbirde kbirde is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
Hi Jim,

I got it to work by changing i=2.

Thanks.
kbirde
  #5 (permalink)  
Old 05-06-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,099

Code:
nawk '
function has(item){
	for(i=1;i<=max;i++)
	{
		if(_[i]==item)
			return 1
	}
	return 0;
}
{
if (NR==FNR)
{
	_[NR]=$0
	max=NR
}	
else{
	flag=1
	for(j=1;j<=NF;j++)
	{
		if(has($j)!=1)
			flag=0		
	}
	if(flag==1)
		print $0	
}
}' file1 file2


perl:

Code:
my %hash;
while(<DATA>){
	chomp;
	$hash{$_}=1;
}
open $fh,"<","file2";
while(<$fh>){
	my $flag=1;
	my @tmp=split;
	for(my $i=0;$i<=$#tmp;$i++){
		$flag=0 if not exists $hash{$tmp[$i]};
	}
	print if $flag == 1;
}
__DATA__
1001
1003
1005
1007


Last edited by summer_cherry; 05-06-2009 at 02:37 AM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0