The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find and FTP multiple files in Korn Shell lambjam UNIX for Dummies Questions & Answers 2 08-13-2007 07:50 PM
Constantly updating log files (tail -f? grep? awk?) nortonloaf UNIX for Dummies Questions & Answers 0 12-03-2006 08:20 PM
Lookup between 2 files ( korn shell ) pavan_test UNIX for Dummies Questions & Answers 17 01-31-2006 01:07 PM
How to process multiple files in Korn Shell stevefox Shell Programming and Scripting 3 11-22-2005 03:39 AM
korn shell + sftp + list files alienET Shell Programming and Scripting 1 03-24-2005 08:12 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-30-2008
Registered User
 

Join Date: Jun 2008
Posts: 18
Korn shell awk use for updating two files

Hi,
I have two text files containing records in following format:
file1 format is:
name1 age1 nickname1 path1
name2 age2 nickname2 path2
file 1 example is:
abcd 13 abcd.13 /home/temp/abcd.13
efgh 15 efgh.15 /home/temp/new/efgh.15
file 2 format is:
name1 age1 nickname1
name2 age2 nickname2
file 2 example:
abcd 13 abcd.13
efgh 21 efgh.21

file 2 is the current (up-to-date) file, and file 1 is the one that needs to update it's records to match file2. file1 contains records that might not exist in file2, and these records need to stay as-is. however, when updating, as in the above example, record efgh needs to be updated to reflect file2's values, so output should look like this:
output file:
abcd 13 abcd.13 /home/temp/abcd.13
efgh 21 efgh.21 /home/temp/new/efgh.21

how can i achieve this with korn shell scripting (using awk maybe?)
thanks
Reply With Quote
Forum Sponsor
  #2  
Old 06-30-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
nawk -f al.awk file2 file1

al.awk:
Code:
FNR==NR { f2[$1]=$0; next }
$1 in f1 {
   n=split(f2[$1], arr, FS)
   for(i=2; i<=n; i++)
     $i=arr[i]
   print
   next;
}
1
Reply With Quote
  #3  
Old 06-30-2008
Registered User
 

Join Date: Jun 2008
Posts: 18
Thanks, but i can't use nawk for some reason. I"m running hpux 11.11
Reply With Quote
  #4  
Old 06-30-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by alrinno View Post
Thanks, but i can't use nawk for some reason. I"m running hpux 11.11
so use either 'awk' or 'gawk'....
Reply With Quote
  #5  
Old 07-08-2008
Registered User
 

Join Date: Jun 2008
Posts: 18
thanks vgersh99...

i'm still having a problem with the script though. It appears to just copy the contents of file1 (the out-of-date file) unaltered. any suggestions as to why?

file1 format: (out-of-date)
abcd 13 abcd.13 /home/temp/abcd.13
efgh 15 efgh.15 /home/temp/new/efgh.15

file2 format: (up-to-date)
abcd 13 abcd.13
efgh 21 efgh.21

output file: (DESIRED)
abcd 13 abcd.13 /home/temp/abcd.13
efgh 21 efgh.21 /home/temp/new/efgh.21

output file: (PRODUCED)
abcd 13 abcd.13 /home/temp/abcd.13
efgh 15 efgh.15 /home/temp/new/efgh.15

note in the PRODUCED output, the values associated with record "efgh" remained the same as they were in file1 (the out-of-date file) rather than updating according to file2's (the up-to-date file) values. any suggestions on how to fix this issue?

thanks a lot
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:47 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0