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
file update piyush_movadiya Shell Programming and Scripting 8 06-27-2007 07:39 AM
How to update a single row in a file with sed brendanf Shell Programming and Scripting 6 08-30-2006 07:44 PM
File update date amne UNIX for Dummies Questions & Answers 2 05-31-2005 08:09 AM
update executable file anent High Level Programming 3 10-19-2001 03:40 PM
Batch file update cfoxwell UNIX for Dummies Questions & Answers 4 10-04-2001 04:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 03-25-2007
ynixon ynixon is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 57
update a file by key

Hi,
I am trying to update a MASTER file by a NEW file which may contain fewer records.
The update should use a key (2 first fields), here is a senario:
MASTER:
a;b;0
a;c;0
a;d;0

NEW:
a;c;1

the result should be:
a;b;0
a;c;1
a;d;0

can you recommend me a way to do it?

10x
Y.N.

Last edited by ynixon; 03-25-2007 at 07:01 PM..
  #2 (permalink)  
Old 03-25-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,508
if you have Python, here's an alternative:
Code:
#!/usr/bin/python
new = open("new").readlines()
fi = open("file").readlines()
for li in fi:
    li = li.strip().split(";")
    for li2 in new:
        li2 = li2.strip().split(";")
        if li2[0] == li[0] and li2[1] == li[1]:
            print ';'.join(li2)
        else:
            print ';'.join(li)
  #3 (permalink)  
Old 03-26-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
Plz give a try on this...

Code:
awk -F";" 'BEGIN {OFS=";"; i=1; while((getline line < "NEW")>0) arr[i++]=line; }  { for(j=1;j<i;j++) { split(arr[j],temp,";"); if (($1==temp[1])&&($2==temp[2])) {$3=temp[3];} }print; }' MASTER
  #4 (permalink)  
Old 03-26-2007
ynixon ynixon is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 57
thanks it is working :)

this is working fast
thank you
  #5 (permalink)  
Old 03-27-2007
ynixon ynixon is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 57
Post a small correction needed

Quote:
Originally Posted by jacoden
Plz give a try on this...

Code:
awk -F";" 'BEGIN {OFS=";"; i=1; while((getline line < "NEW")>0) arr[i++]=line; }  { for(j=1;j<i;j++) { split(arr[j],temp,";"); if (($1==temp[1])&&($2==temp[2])) {$3=temp[3];} }print; }' MASTER
I forgot to mention that I also need to add to the result new records from NEW that does not exist in MASTER.
for example:

MASTER:
a;b;0
a;c;0
a;d;0

NEW:
a;c;1
a;e;2

the result should be:
a;b;0
a;c;1
a;d;0
a;e;2
  #6 (permalink)  
Old 03-27-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by ynixon
I forgot to mention that I also need to add to the result new records from NEW that does not exist in MASTER.
for example:

MASTER:
a;b;0
a;c;0
a;d;0

NEW:
a;c;1
a;e;2

the result should be:
a;b;0
a;c;1
a;d;0
a;e;2
Code:
awk -F";" ' BEGIN {OFS=";"; while( getline < "NEW" ) arr[$1";"$2]=$3; }
{  if( arr[$1";"$2] !~ /^ *$/ ) { $3=arr[$1";"$2]; print; delete arr[$1";"$2] } 
   else print 
}
END { 
for ( key in arr ) {
if( arr[key] !~ /^ *$/ ) { print key";"arr[key] } 
} } ' MASTER
  #7 (permalink)  
Old 03-27-2007
ynixon ynixon is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 57
Thumbs up Thanks :D

It is even working faster
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 08:22 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