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
need some help..Comparison bluesilo Shell Programming and Scripting 0 02-23-2008 04:43 PM
Comparison of 2 files in UNIX Dana Evans UNIX for Dummies Questions & Answers 32 11-21-2007 07:05 AM
String Comparison between two files using awk rudoraj Shell Programming and Scripting 7 07-25-2006 12:04 PM
dir comparison help ghazi Shell Programming and Scripting 5 12-20-2004 05:54 PM
comparison cnf Filesystems, Disks and Memory 2 05-14-2002 02:52 PM

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 07-25-2006
jerome Sukumar jerome Sukumar is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 22
Post Comparison of two files in awk

Hi,
I have two files file1 and file2 delimited by semicolon,
And I want to compare column 2 and column3 of file1 to column3 and column 4 in file2.

file1
--------
abc;cef;155.67;143_34;
def;fgh;146.55;123.3;
frg;hff;134.67;;
yyy;fgh;134.78;35_45;

file 2
---------
abc;cef;155.09;;
abc;cef;155.67;143_34;
asd;;;123;
def;fgh;145.6;123.3;
def;fgh;146.55;123.3;
frg;hff;134.67;;

Successfile1
------------
abc;cef;155.67;143_34;
def;fgh;146.55;123.3;

Failfile1
-----------
frg;hff;134.67;;
yyy;fgh;134.78;35_45;

Can anyone help me with a script.
  #2 (permalink)  
Old 07-25-2006
girish.karulkar girish.karulkar is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 4
Hi Jerome

First of all wht is see is col2 of file 1 is text & col3 of file2 is number,
so how u r going to compare?

but still you can use somewhat this way

#!/usr/bin/ksh

cut -d";" -f2 file1 >> tmpf2.txt
echo
cut -d";" -f3 file2 >> tmpf3.txt

diff tmpf2.txt tmpf3.txt

cut -d";" -f3 file1 >> tmpf3.txt
echo
cut -d";" -f4 file2 >> tmpf4.txt

diff tmpf3.txt tmpf4.txt

rm tmpf[0-9].txt

  #3 (permalink)  
Old 07-25-2006
jerome Sukumar jerome Sukumar is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 22
rephrase

Sorry girish,

I have given the column info wrongly,
Its col3 and col4 of file1 to col3 and col4 of file2 comparison.
  #4 (permalink)  
Old 07-25-2006
grial's Avatar
grial grial is offline Forum Advisor  
El UNIX es como un toro
  
 

Join Date: Jun 2006
Location: Madrid (Spain)
Posts: 531
Perhaps this is what you want, but I'm not sure if I've understood you

Code:
#!/bin/bash

comp1=($(cat text1.txt | cut -d\; -f 3,4))
comp2=($(cat text2.txt | cut -d\; -f 3,4))

for str in ${comp1[*]}; do
   i=0
   while (( $i < ${#comp2[*]} )); do
      if [[ $str = ${comp2[i]} ]]; then
         cat text1.txt | grep $str
      fi
      (( i += 1 ))
   done
done

Regards.
  #5 (permalink)  
Old 07-25-2006
jerome Sukumar jerome Sukumar is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 22
Problem.

Hi Grial,
Thanks for your prompt and quick response.

The script works for comparing two cols i.e., col 3 and col4 of two files.

If i try to try to compare only col3 of two files,
I am getting redundant records.

Eg:
My File1 consists of 100 records and
file2 consists of 238 records.If i try to compare,file1 and file2 I got 116 records as my o/p
in the console.Can u suggest me,how to rectify this.
  #6 (permalink)  
Old 07-25-2006
grial's Avatar
grial grial is offline Forum Advisor  
El UNIX es como un toro
  
 

Join Date: Jun 2006
Location: Madrid (Spain)
Posts: 531
Again, I don't know if I've understood. Do you mean you could have duplicate records on file2? Or, Do you want only the first ocurrence? If this is teh case, try:

Code:
#!/bin/bash

comp1=($(cat text1.txt | cut -d\; -f 3,4))
comp2=($(cat text2.txt | cut -d\; -f 3,4))

for str in ${comp1[*]}; do
   i=0
   while (( $i < ${#comp2[*]} )); do
      if [[ $str = ${comp2[i]} ]]; then
         cat text1.txt | grep $str
         break
      fi
      (( i += 1 ))
   done
done

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 07:43 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