update a file by key


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting update a file by key
# 1  
Old 03-25-2007
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 08:01 PM..
# 2  
Old 03-25-2007
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  
Old 03-26-2007
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  
Old 03-26-2007
thanks it is working :)

this is working fast Smilie
thank you
# 5  
Old 03-27-2007
Java 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  
Old 03-27-2007
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  
Old 03-27-2007
MySQL Thanks :D

It is even working faster Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Getting count of Key in another file

Hi All, I need to find the count of key occurrence from one file corresponding to another file. Any help please. I am planning to read the CUST_ID in a while loop and do a grep , is there a way to do easier CUST_ID 677582806078 687582821181 687582828910 687582834580 687582834849... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

2. Shell Programming and Scripting

Update a specific field in file with Variable value based on other Key Word

I have an input file with A=xyz B=pqr I would want the value in Second Field (xyz or pqr) updated with a value present in Shell Variable based on the value passed in the first field. (A or B ) while read line do NEW_VALUE = `some functionality done on $line` If $line=First Field-... (1 Reply)
Discussion started by: infernalhell
1 Replies

3. Shell Programming and Scripting

How to extract information a file according key id in another file?

hi, i have a large file containing the detailed information of a bunch of keys like this: JAT_0001 contig102_342_3_n2 contig102_342 atgcacgacta 30 50 20... (11 Replies)
Discussion started by: the_simpsons
11 Replies

4. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

5. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

6. Solaris

Solaris 8 ssh public key authentication issue - Server refused our key

Hi, I've used the following way to set ssh public key authentication and it is working fine on Solaris 10, RedHat Linux and SuSE Linux servers without any problem. But I got error 'Server refused our key' on Solaris 8 system. Solaris 8 uses SSH2 too. Why? Please help. Thanks. ... (1 Reply)
Discussion started by: aixlover
1 Replies

7. UNIX for Dummies Questions & Answers

Remove VI encryption key from file

Hi There, I have set encryption key to my file using :X command. Now that I no more need encryption key to the file, I just want to delete/remove the encryption key. I have gone through many source but in vain. None of the source provided me with the solution that I am looking for. I... (2 Replies)
Discussion started by: grc
2 Replies

8. Shell Programming and Scripting

Using Key to get data from second file

I posted a problem last week that had essentially two steps. Someone was kind enough to help me with the first step, but beacuse I didn't explain things well, left out the second step. I'm required to work in C Shell. I deeply appreciate any help, since I've never worked in a shell language... (4 Replies)
Discussion started by: bassmaster
4 Replies

9. UNIX for Dummies Questions & Answers

Pressing backspace key simulates enter key

Hi, Whenever i press the backspace key, a new line appears, i.e. it works like a enter key. :confused: Thanks (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies
Login or Register to Ask a Question