![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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 |
|
||||
|
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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|