Computing.Net > Forums > Unix > Update files with awk

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.

Update files with awk

Reply to Message Icon

Name: ccooper03
Date: October 26, 2008 at 09:45:51 Pacific
OS: Unix
CPU/Ram: Unknown
Product: Dell
Comment:

I have two files

I need to create a third file (file3) that contains third field in file2 appended to the beginning of the matching record of file1. If there is no match, the inserted value = the value not found.

Here is an example of file1

“$AAA”,”SYSA”,”VALU1”
“$ABA”,”SYSA”,”VALU1”
“0AAA”,”SYSA”,”VALU1”
“2AAA”,”SYSA”,”VALU1”
“3AAA”,”SYSA”,”VALU1”


Here is an example of file2

$AAA,1111,P1
$ABA,1111,P200
0AAA,1111,P35
3AAA,1111,P12

Here is the desired output

“P1”,“$AAA”,”SYSA”,”VALU1”
“P200”,“$ABA”,”SYSA”,”VALU1”
“P35”,“0AAA”,”SYSA”,”VALU1”
“2AAA”,“2AAA”,”SYSA”,”VALU1”
“P12”,“3AAA”,”SYSA”,”VALU1”

Note – in the 4th record, there was no matching value, so the output = the value that could not be found.

I’m trying to use gawk. Any help would be greatly appreciated.

Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 26, 2008 at 21:21:31 Pacific
Reply:

I read file2 into an associative array. Since file2's fields are not surrounded by double quotes, that's what this line is for:

a["\""$3"\""]="\""$1"\""

Let me know if you have any questions.

#!/bin/ksh


awk ' BEGIN { FS=",";
while(getline < "file2" > 0)
a["\""$3"\""]="\""$1"\""
}
{
for (c in a)
if(a[c] == $1)
{
bf=c
break
}
else
bf=$1

printf("%s,%s\n", bf, $0)

} ' file11



0

Response Number 2
Name: ccooper03
Date: October 27, 2008 at 06:05:26 Pacific
Reply:

Thank you for your help.

I'm getting a syntax error ( I think it is the version of AWK). I will play around with it.


0

Sponsored Link
Ads by Google
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 Unix Forum Home


Sponsored links

Ads by Google


Results for: Update files with awk

trouble merging two files with awk www.computing.net/answers/unix/trouble-merging-two-files-with-awk/7937.html

Read tow files with AWK www.computing.net/answers/unix/read-tow-files-with-awk/6634.html

Renaming files with awk www.computing.net/answers/unix/renaming-files-with-awk/8036.html