Korn shell awk use for updating two files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Korn shell awk use for updating two files
# 1  
Old 06-30-2008
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
# 2  
Old 06-30-2008
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

# 3  
Old 06-30-2008
Thanks, but i can't use nawk for some reason. I"m running hpux 11.11
# 4  
Old 06-30-2008
Quote:
Originally Posted by alrinno
Thanks, but i can't use nawk for some reason. I"m running hpux 11.11
so use either 'awk' or 'gawk'....
# 5  
Old 07-08-2008
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

korn shell remove files question

how do you show each filename in a giving directory and delete the specific file in korn script i was thinking using ls rm ? but i cant make it work (0 Replies)
Discussion started by: babuda0059
0 Replies

2. Shell Programming and Scripting

Comparing 2 files with awk and updating 2nd file

file1: (unique files) 1 /pub/atomicbk/catalog/catalog.gif 693 2 /pub/atomicbk/catalog/home.gif 813 3 /pub/atomicbk/catalog/logo2.gif 12871 4 /pub/atomicbk/catalog/sleazbk.html 18338 file2: (duplicate filenames allowed) 28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813... (2 Replies)
Discussion started by: jontjioe
2 Replies

3. Shell Programming and Scripting

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... (11 Replies)
Discussion started by: mecano
11 Replies

4. UNIX for Dummies Questions & Answers

Initializing files to empty in korn shell

hello, i want to know how to initialize a file to an empty one in korn shell scripting? i'm using a file name and building it during a while loop using >>. The problem occurs when the file is not empty before reaching the while loop. therefore, i want to initialize it before the loop to get... (6 Replies)
Discussion started by: alrinno
6 Replies

5. Shell Programming and Scripting

substr() thru awk Korn Shell Script

Hi, I am new stuff to learn substr() function through awk for writing the Korn shell script. Is there a way to copy from XXXX1234.ABCDEF to XXX1234 file names without changing both data files? I appreciate your time to response this email. Thanks, Steve (4 Replies)
Discussion started by: sbryant
4 Replies

6. Shell Programming and Scripting

Korn shell and awk question

I am modifying a Korn shell script in using the Exceed (Solaris 10 environment). My task is to read in a .txt file with dates arranged like this (01-Sep-2006). I am to read each line and take the dates, compare them to a benchmark date and depending on if it is older than the date or the date and... (6 Replies)
Discussion started by: mastachef
6 Replies

7. UNIX for Dummies Questions & Answers

Constantly updating log files (tail -f? grep? awk?)

I have a log file which is continuously added to, called log.file. I'd like to monitor this file, and when certain lines are found, update some totals in another file. I've played around with tail -f, grep, and awk, but can't seem to hit the right note, so to speak. The lines I'm... (0 Replies)
Discussion started by: nortonloaf
0 Replies

8. UNIX for Dummies Questions & Answers

Lookup between 2 files ( korn shell )

Hi All., i have a problem. I hope i can get some help on this issue here; i have 2 txt files say file1 and file 2 file1 has; WLMT:XXXXXXXX:cp DOLR:YYYYYYY:ascii,unblock WLG:TTTTTTT:dd:73:ascii,unblock MAR:SSSSSS:dd:152:ascii,unblock GGG:QQQQQQQQQQ:112:ascii,unblock EIE:CCCCCCCC:cp... (17 Replies)
Discussion started by: pavan_test
17 Replies

9. Shell Programming and Scripting

korn shell + sftp + list files

Hello!!! I need a korn shell script in AIX that inside sftp environment, changes a remote directory, lists the files inside it, and stores in an array. I got it working before make a sftp, but after.. I can't.. The way it is, it lists the files in local path... so.. not what I want, but... (1 Reply)
Discussion started by: alienET
1 Replies

10. Shell Programming and Scripting

AWK question in the KORN shell

Hi, I have two files with the following content: gmrd.txt 235649;03;2563;598 291802;00;2563;598 314634;00;235649;598 235649;03;2563;598 393692;00;2563;598 411805;00;2563;598 411805;00;2563;598 235649;03;2563;598 414037;00;2563;598 575200;00;2563;598 70710;00;2563;598... (11 Replies)
Discussion started by: penfold
11 Replies
Login or Register to Ask a Question