The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare 2 files.. amon Shell Programming and Scripting 8 06-23-2008 10:34 AM
compare two files charandevu Shell Programming and Scripting 7 03-30-2008 03:20 PM
compare two txt files space13 Shell Programming and Scripting 8 09-22-2006 09:40 AM
compare files and beyond MizzGail UNIX for Dummies Questions & Answers 2 04-25-2003 01:34 PM
compare files ingunix UNIX for Dummies Questions & Answers 3 05-24-2001 11:44 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 03-10-2007
kharen11 kharen11 is offline
Registered User
  
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Compare files

Hi Masters! I know this problem is quite difficult.
I have two files that looked like this:

File1
mary a b d
anne d e
jane g h
sam a


File2
role1 a d c d
role2 e f g h
role3 a b d
role4 a d e
role5 g h


It will first look into file1 then compare to all entries in file2 regardless of arrangement.

The output should be:
mary role1 role3
anne role 4
jane role2 role5
sam role1 role3 role4


I've tried some other way to do this but unsuccessful. Can it be done in UNIX? Please help. Thanks!
  #2 (permalink)  
Old 03-10-2007
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
This looks a lot like homework, so I will not give an answer.

Post what you have tried already and maybe someone will point out where you are going wrong.
  #3 (permalink)  
Old 03-10-2007
kharen11 kharen11 is offline
Registered User
  
 

Join Date: Mar 2007
Location: Manila
Posts: 25
It's not a homework. Actually I tried using excel to manipulate the files but I'm not familiar with macros so I hope someone can help me do it in UNIX.

This will be a repeated process for me and having a right code will really help me a lot. Unfortunately my limited knowledge in UNIX wont help me too.
  #4 (permalink)  
Old 03-10-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by kharen11
Hi Masters! I know this problem is quite difficult.
I have two files that looked like this:

File1
mary a b d
anne d e
jane g h
sam a


File2
role1 a d c d
role2 e f g h
role3 a b d
role4 a d e
role5 g h


It will first look into file1 then compare to all entries in file2 regardless of arrangement.

The output should be:
mary role1 role3
anne role 4
jane role2 role5
sam role1 role3 role4


I've tried some other way to do this but unsuccessful. Can it be done in UNIX?

What are the criteria? This script looks for all lines in File2 that contain any of the letters after the name, but the ouput doesn't match yours:

Code:
while read name a b c d e
do
  [ -n "$a$b$c$d$e" ] &&
  result=$( grep ${a:+-e " $a"} ${b:+-e " $b"} ${c:+-e " $c"} \
             ${d:+-e " $d"} ${e:+-e " $e"} File2 | cut -d ' ' -f1)
  set -- $result
  echo "$name" $result
done < File1
  #5 (permalink)  
Old 03-10-2007
kharen11 kharen11 is offline
Registered User
  
 

Join Date: Mar 2007
Location: Manila
Posts: 25
The files on file1 can go over several rows and columns as well as for file2. For example, mary has a, b and d. It will then search the file2 that contains a, b and d (it should be all not any) which in this case role1 and role3. Then it will begin searching for second entry in file1 who is anne, and so on.
  #6 (permalink)  
Old 03-11-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
Code:
#! /opt/third-party/bin/perl

open(FILE, "<", b) || die ("Unable to open file. <$!>\n");

while( <FILE> ) {
  chomp;
  @split_arr = split(/ /, $_);
  my $dump;
  for( my $i = 1; $i < $#split_arr + 1; $i++ ) {
    $dump .= $split_arr[$i];
  }
  $fileHash{$split_arr[0]} = $dump;
}

close(FILE);

open(FILE, "<", a) || die ("Unable to open file. <$!>\n");

while( <FILE> ) {
  chomp;
  @split_arr = split(/ /, $_);
  my $dump;
  for( my $i = 1; $i < $#split_arr + 1; $i++ ) {
    $dump .= $split_arr[$i];
  }
  print "$split_arr[0] ";
  foreach my $key ( keys %fileHash ) {
    if( $fileHash{$key} =~ m/$dump/ ) {
      print "$key ";
    }
  }
  print "\n";
}

close(FILE);

exit 0
I have onething to be clarified,

from the examples provided,
for mary only role3 would match
and not
both role3 and role1

Could you please check and confirm that ?
  #7 (permalink)  
Old 03-11-2007
kharen11 kharen11 is offline
Registered User
  
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Quote:
Originally Posted by matrixmadhan
Code:
#! /opt/third-party/bin/perl

open(FILE, "<", b) || die ("Unable to open file. <$!>\n");

while( <FILE> ) {
  chomp;
  @split_arr = split(/ /, $_);
  my $dump;
  for( my $i = 1; $i < $#split_arr + 1; $i++ ) {
    $dump .= $split_arr[$i];
  }
  $fileHash{$split_arr[0]} = $dump;
}

close(FILE);

open(FILE, "<", a) || die ("Unable to open file. <$!>\n");

while( <FILE> ) {
  chomp;
  @split_arr = split(/ /, $_);
  my $dump;
  for( my $i = 1; $i < $#split_arr + 1; $i++ ) {
    $dump .= $split_arr[$i];
  }
  print "$split_arr[0] ";
  foreach my $key ( keys %fileHash ) {
    if( $fileHash{$key} =~ m/$dump/ ) {
      print "$key ";
    }
  }
  print "\n";
}

close(FILE);

exit 0
I have onething to be clarified,

from the examples provided,
for mary only role3 would match
and not
both role3 and role1

Could you please check and confirm that ?

Yes, you're right
Can you tell me where in the code is the first file and the second file?
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:23 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