Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, i'm trying to compare two files. The content of the files are organized in the same way, first field is the name of the item, second field is the price. Eg.
oldpricelist.txt
apples,2
bananas,3
grapes,1newpricelist.txt
apples,2
bananas,4
grapes,1
melon,5What i'm trying to do is, go through "newpricelist" line by line, firstly checking to see if it exists in the "oldpricelist", if it doesn't ignore, but if it does exist, then check if the price is the same, then print a message if the prices does or doesn't match.
So far, i have written an awk script to get me as far as finding what exists in the "oldpricelist" but once i get to this point, i don't know how to proceed to comparing the PRICE, because i can't figure out how to use arrays in awk properly.
Here's what i have:
awk -F, 'BEGIN {
while ((getline < "oldpricelist.txt") > 0)
array[$1] = $1
OFS=","}
{if (array[$1])
#if a match of fruit is found, proceed to compare price
if(???)
print "price match"
else
print "new price"
}' newpricelist.txtAny help is much appreciated. Thanks.

Since my OS is Solaris, I am using nawk. Let me know if you have any questions:
nawk ' BEGIN { FS=","; while ((getline < "oldpricelist.txt") > 0) myarray[$1] = $2 } { if($1 in myarray) if(myarray[$1] == $2) printf("%s: price match\n", $1) else printf("%s: new price \n", $1) }' newpricelist.txt

![]() |
sed: conditional newline ...
|
csv processing using shel...
|
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |