On 2/7/2008 2:53 PM, Combatwombat wrote:
> Janis Papanagnou wrote:
>
>>Ed Morton wrote:
>>
>>>On 2/7/2008 1:45 AM, Combatwombat wrote:
>>>
>>>
>>>>Thanks for your reply, Ed!
>>>
>>>
>>>You're welcome, but in future please leave enough context so your post
>>>stands
>>>alone as this is netnews not a web forum.
>>>
>>>
>>>
>>>>The old.csv has numerous lines like this:
>>>>
>>>>AAA80008 Automobile fins AAA CS 0
>>>>
>>>>
>>>>the replacement csv has lines like this:
>>>>AAA Archies Automobiles and Airplanes
>>>>
>>>>
>>>>so by replacing the AAA in the record of old.csv we end up with:
>>>>
>>>>
>>>>AAA80008 Automobile fins Archies Automobiles and
>>>>Airplanes CS 0
>>>>
>>>>Thanks!
>>>
>>>
>>>If those are tabs between the fields then all you need is:
>>>
>>>awk 'BEGIN{FS=OFS="\t"} NR==FNR{a[$1]=$2;next} {$3=a[$3];print}'
>>>replacement.csv
>>>old.csv
>>>
>>> Ed.
>>>
>>
>>It seems to fit for the OP, but mind that {$3=a[$3];print} will
>>_always_ replace $3, even in cases where there's no substitute for
>>the third field defined in the map; in that case the third field
>>would be blanked.
>>
>>Janis
>
>
> Yes, you are right Janis.
Yes, all you need is a tweak to check for the key field being present in
the array:
awk 'BEGIN{FS=OFS="\t"} NR==FNR{a[$1]=$2;next} $3 in a{$3=a[$3]} {print}'
replacement.csv old.csv
> I soon discovered that, and reworked the orginal code I had using
getline:
>
> BEGIN { FS = ":" };{
>
> if($10 =="y"||$10=="Y"){ # begin process of extraction because
ALLOWSELL=y
> qty=$6+$8;
> if (($37 =="N"||$37=="n")&&qty<=0){
> status=9
> }else{ status=1;
> }
>
> {
> #Reassign Globals
> NewNR=NR; NewFNR=FNR
> for (i=1; i<=37; i++){
> store[i]=$i
>
> }#end for loop of storing data
> while ((getline < "replacement.csv") > 0)# load replacements file
> if ($1==store[4]){ manf=$2 } #select the replacement
> close("replacement.csv") #close replacement file
>
> NR=NewNR; FNR=NewFNR
> for (i=1; i<=37; i++){
> $i=store[i]
>
> }#end for loop of restoring data
> $4=manf #assign the replacement
>
>
> print
>
$1,":",$1".jpg",":",$1,":",$4,":",$2,":","url",":",$7,":",$7,":",qty,":",$5,":",status,"EOREOR";
>
> }
> }
>
>
> }
>
> I'm betting this could probably be cleaned up code-wise, but it is easy
> to read and debug at the moment.
>
> The entire project works well at converting an aging DBF based
> accounting system data into data usable by Zencart. If anyone is
> interested I will be posting my findings at
> http://combatwombat.7doves.com
soon.
>
> Thanks for your help everyone!
You REALLY need to read http://tinyurl.com/yn9ka9
if you're still
considering
using getline. Among other problems, the above will read the whole of
"replacement.csv" once for each record in "old.csv".
Ed.


|