Computing.Net > Forums > Programming > Korn shell awk use for updating two

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Korn shell awk use for updating two

Reply to Message Icon

Name: alrinno
Date: June 30, 2008 at 09:23:47 Pacific
OS: UNIX
CPU/Ram: N/A
Product: Any
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: June 30, 2008 at 10:04:19 Pacific
Reply:


awk 'FNR==NR{
a[$1] = $0;
next
}
(( $1 in a ) && ( a[$1] !~ $3)) {
split(a[$1],b," ")
gsub( $1".*$" ,b[3],$4)
printf "%s %s\n" ,a[$1] ,$4
next
}1
' file2 file1


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Korn shell awk use for updating two

using for loops to interate unicode www.computing.net/answers/programming/using-for-loops-to-interate-unicode/12858.html

Swtich to use for yes and no prompt www.computing.net/answers/programming/swtich-to-use-for-yes-and-no-prompt/16104.html

Help with Assignment www.computing.net/answers/programming/help-with-assignment/18340.html