![]() |
|
|
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 |
| Sed Help in Updating something only in one particular file. | bisla.yogender | Shell Programming and Scripting | 4 | 03-24-2008 11:48 AM |
| TO know whether file is updating or not | raj333 | Shell Programming and Scripting | 2 | 11-05-2007 05:29 PM |
| Updating file in loop | braindrain | Shell Programming and Scripting | 4 | 06-20-2007 02:37 PM |
| Why my sar is not updating the output file. | skneeli | UNIX for Advanced & Expert Users | 1 | 11-12-2006 08:29 PM |
| Updating a File in a Zip Archive | dbridle | AIX | 6 | 09-27-2006 04:29 PM |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
awk updating one file with another, comparing, updating
Hello,
I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea 3495; test filed 4578; test filec 7689; test filex 8978; results: test filea 3495; test filed 4578; test filec 7689; test filex 8978; test filey 9978; test filez 12300; comparison in based on last field (field $3), new content from file2 (here content with "key" 8978 is new) should be added to final output and content that is different in file2 (test filed 4578; here) should replace file1 one. here is where I am now: awk 'NF { key=$NF;keys[key]++ } NR == FNR { key1[key] = $NF ORS;rec1[key] = $0 ORS;next } { key2[key] = $NF ORS;rec2[key] = $0 ORS;next } END { for (k in keys) { if (key1[k] == key2[k]) { print rec2[k] } else { print rec1[k] } } }' $file1 $file2 > $file1.updated for readability: awk ' NF { key=$NF; keys[key]++ } NR == FNR { key1[key] = $NF ORS; rec1[key] = $0 ORS; next } { key2[key] = $NF ORS; rec2[key] = $0 ORS; next } END { for (k in keys) { if (key1[k] == key2[k]) { print rec2[k] } else { print rec1[k] } } }' $file1 $file2 > $file1.updated but.. this doesn't work well :/ |
| Bookmarks |
| Tags |
| solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|