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
awk - comparing files dbrundrett Shell Programming and Scripting 6 01-18-2009 10:51 PM
Comparing two files superstar003 Forum Support Area for Unregistered Users & Account Problems 1 05-08-2008 04:34 AM
Comparing two files.. padarthy Shell Programming and Scripting 1 08-29-2007 09:01 AM
Comparing two files... paqman Shell Programming and Scripting 12 08-08-2007 04:45 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 11-21-2006
jerome Sukumar jerome Sukumar is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 22
comparing two files using awk.

Hi All,

a new bie to awk,

How to compare substring of col1,file 1 with
col2file2 and get file1contents+col3file2 as output.

file1
-----
kumarfghh,23,12000,5000
rajakumar,24,14000,2500
rajeshchauhan,25,16000,2600
manoj,26,17000,2300

file 2
--------
123,kumar,US,
123,sukumar,UK
123,raj,Germany
40,rajesh,Australia
40,jerome,swiss
40,rakesh,india

output
-----------------------
kumarfghh,23,12000,5000,kumar,US
rajakumar,24,14000,2500,raj,Germany
rajeshchauhan,25,16000,2600,rajesh,Australia
manoj,26,17000,2300,,

Note:so if nothing matched i should get a default value as null.

Please help me,as i have been trying very hard to acheive
  #2 (permalink)  
Old 11-21-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,411
Try this awk program called "eg.awk" ...
Code:
        BEGIN {
                OFS = FS = ","
        }

        NR == FNR       {
                b[$2] = $3
                next
        }

        {
                e = ""
                for (x in b) {
                        if (match($1, x)) {
                                if (RSTART == 1 && RLENGTH > length(e)) {
                                        e = x
                                }
                        }
                }
                print $0, e, b[e]
        }

...run it like this...
Code:
awk -f eg.awk file2 file1

...which gives...
Code:
kumarfghh,23,12000,5000,kumar,US
rajakumar,24,14000,2500,raj,Germany
rajeshchauhan,25,16000,2600,rajesh,Australia
manoj,26,17000,2300,,

  #3 (permalink)  
Old 11-21-2006
jerome Sukumar jerome Sukumar is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 22
Can u explain

Thanks a lot ygor
great script.works fine
As iam new bie to awk,can u explain me ygor.

Thanks and Regards,
sukumar
  #4 (permalink)  
Old 11-21-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,411
Which part don't you understand?
  #5 (permalink)  
Old 11-22-2006
jerome Sukumar jerome Sukumar is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 22
Help plz

hi ygor,

I have understood the script,but one more issue is that the script is taking more time if my file 1 and file2 exceeds more than 5000 records,

As we are taking file2 in array,what will be limitation of the array.(bcoz in future if my file 2 increased,tats why).

Can u give some tips how to improve the performance.

Regards,
Sukumar.
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 04:21 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