I have a large comma delimited file from which i have to extract a specific column, compare it against a static file and replace the column value with the matching column from the static file.
The following are the files:
Main file:
mainfile.csv
test1234,,real,,,,open,,,,EU,,,,23/03/2012 12:03:00,,,,GB,,,,1187,0,B,TRY,10000,15/07...
test1234,,real,,,,open,,,,EU,,,,23/03/2012 12:03:00,,,,GB,,,,1187,0,B,FDG,10000,15/07...
test1234,,real,,,,open,,,,EU,,,,23/03/2012 12:03:00,,,,GB,,,,1187,0,B,HGC,10000,15/07...
test1234,,real,,,,open,,,,EU,,,,23/03/2012 12:03:00,,,,GB,,,,1187,0,B,MDL,10000,15/07...
Static file.
reffile
CODE,PRODUCT
UGE72453,TRY
NGU76290,FDG
HRY82662,HGC
LQY35689,MDL
I now want to replace the bolded 26th column in the main file with the code from the static file
The main file has about 1000 lines but only has the aforementioned [products.
I am using the following code but only a
OLDIFS=$IFS
IFS=","
while read pr1 pr2
do
for i in `cat mainfile.csv | awk 'BEGIN { FS = ","; } { print $26}'`
do
if [[ "$i" == *"$pr2"* ]]; then
echo $i $pr1
# sed 's/$i/$pr1/' $i;
fi
done
done < reffile
IFS=$OLDIFS
When i execute the above code, only some of the lines get their value replaced.
Can you please advise check for pr2 in the for loop and replace by the corresponding pr1 in from the while? I would appreciate if someone can even advise me on a better way of doing this