![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
|
|
||||
|
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.. |
|
||||
|
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)
|
|
||||
|
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
|
|
||||
|
Quote:
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 |
|
||||
|
Quote:
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|