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 > 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
compare two files and to remove the matching lines on both the files shellscripter Shell Programming and Scripting 4 06-20-2009 08:48 AM
matching string in two files of different length ad_meis Shell Programming and Scripting 2 05-19-2009 10:49 AM
Matching and combining two files sickboy Shell Programming and Scripting 3 07-17-2008 12:20 PM
Matching and combining two files p3t3r Shell Programming and Scripting 5 05-20-2008 10:16 AM
Find matching lines between 2 files jojojmac5 UNIX for Dummies Questions & Answers 5 01-18-2007 01:06 PM

Reply
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 07-01-2009
kylle345 kylle345 is offline
Registered User
  
 

Join Date: May 2009
Posts: 46
Matching 2 files

Hi,

I am able to modify files (one file) but I have a hard time working with multiple files simultaneously.

So I have two files. Basically I want to match two files based on certain columns. Both files are tab-seperated.

File1 looks something like this:
Code:
num1  89  george
num4  78  bob
File 2 is a much larger file and looks somethign liek this:
Code:
num1  3    lion
num1  13  tiger
num1  84  bear
num4  3    house
num4  19  dog
num4  77  mouse
So what I want to do is match column 1 from Files1 and 2. I also want to substract column 2 from both files. So if they are a match, I want to print (into a seperate file) the row with the smallest difference for column 2.

So for my example above, the output file would look like this

num1 5 george bear
num4 1 bob mouse


thanks

Last edited by Yogesh Sawant; 07-05-2009 at 10:29 AM.. Reason: added code tags
  #2 (permalink)  
Old 07-02-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
What have you tried so far?
  #3 (permalink)  
Old 07-02-2009
lathavim lathavim is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 73
See if this helps.
Code:
while read value1
do
a=`echo $value1 | cut -d ' ' -f1`
b=`echo $value1 | cut -d ' ' -f2`
c=`echo $value1 | cut -d ' ' -f3`
while read value2
do
a1=`echo $value2 | cut -d ' ' -f1`
b1=`echo $value2 | cut -d ' ' -f2`
c1=`echo $value2 | cut -d ' ' -f3`
if [ "$a" = "$a1" ]
then
b2=`expr $b - $b1`
echo $a $b2 $c $c1 >>outfile
fi
done<file2
done <file1
sort -o outfile2 -2 outfile
while read out1
do
x=`echo $out1 | cut -d ' ' -f1`
grep $x outfile2 | head -1 >>finalout
grep -v $x outfile2 >tmp
mv tmp outfile2
done<outfile2

Last edited by Yogesh Sawant; 07-05-2009 at 10:28 AM.. Reason: added code tags
  #4 (permalink)  
Old 07-02-2009
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120
Try this...


Code:
while read value1
do
        a=`echo $value1 | cut -d ' ' -f1`
        b=`echo $value1 | cut -d ' ' -f2`
        c=`echo $value1 | cut -d ' ' -f3`
        x=`grep "$a" file2|sort -nr -k2,2|head -1|cut -d' ' -f1`
        y=`grep "$a" file2|sort -nr -k2,2|head -1|cut -d' ' -f2`
        z=`grep "$a" file2|sort -nr -k2,2|head -1|cut -d' ' -f3`
        Diff=`expr $b - $y`
        echo "$a $Diff $c $z" >>outfile
done <file1
Note:
file1 - your first file
file2 - your second file
outfile - the result file
  #5 (permalink)  
Old 07-02-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
I had the solution ready, but wanted you to give it a try...
Code:
find_corresponding_least_val()
{
   cat file2 | grep "$_key" | awk '{
      if((val-$2) < _diff) {
         _diff=val-$2; args=$3
      }
      else
         _diff=val-$2
   }
      END {
         printf("%s\t%s\t%s\n", key, _diff, args)
   }' key="$_key" val="$_val"
}

while read line
do
   _key=`echo $line | awk '{print $1}`
   _val=`echo $line | awk '{print $2}`
   _args=`echo $line | awk '{print $3}`
   find_corresponding_least_val $_key $_val
done < file1
Reply

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 02:29 PM.


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