Good day all, Prosperous new year to all.
I have a file using the pipe character as field delimiter. The aim is to prepend a certain field, in this case field 4, with either a C or R depending on the values of the fields 2 and 3.
File in question:
11975528|0202|INVOICES| 211311|2020/12/11|00893139P| | 34.90 | .00 | 5.24 | 40.14 |MRS L JAMES |Pr#:558392 M/Aid:PR
11975529|0202|INVOICES| 211312|2020/12/11|00893140P| | 34.90 | .00 | 5.24 | 40.14 |MISS M KHOZA |Pr#:558393 M/Aid:PR
11975530|0202|INVOICES| 211311|2020/12/11|00893142P| | 34.90 | .00 | 5.24 | 40.14 |MRS L JAMES |Pr#:558392 M/Aid:PR
11975531|0202|C/NOTES | 211311|2020/12/11|00893142P|20/12/11 | 34.90-| .00 | 5.24-| 40.14-|MRS L JAMES |Pr#:558392 M/Aid:PR
11975532|0202|C/NOTES | 211310|2020/12/11|00893136P|20/12/11 | 115.90-| .00 | 17.39-| 133.29-|MR S ERASMUS |Pr#:558391 M/Aid:PR
I have passed passed the file into a while read loop, using awk to separate the fields and sed to replace the values:
if [ -f $datext ]; #Check if the extract file exits
then
cat $datext | while IFS= read line
do
BCHNO=`echo $line | awk -F"|" '{print $2}'` #Get BRANCH Number
TYPE=`echo $line | awk -F"|" '{print $3}'` #Get Transation type e.g. invoice/credit note
DEBNO=`echo $line | awk -F"|" '{print $4}'` #Get debtor account number
if [ $BCHNO -eq 0202 ] && [ "$TYPE" == "INVOICES" ];
then
sed -i "s/$DEBNO/C$DEBNO/" $datext
elif [ $BCHNO -eq 0202 ] && [ "$TYPE" == "C/NOTES" ];
then
sed -i "s/$DEBNO/R$DEBNO/" $datext
fi
done
When running the above, where there are multiple transactions for the same debtor it keeps prepending the C:
11975534|0202|INVOICES| CCCCCC 911202|2020/12/11|00893144P| | 21.17 | .00 | 3.18 | 24.35 |LEVY CONTROL |Pr#:558384 M/Aid:PR
And the C/NOTES are ignored and also prepended with a C:
11975540|0202|C/NOTES | C 558379|2020/12/11|00893149P|20/12/11 | 173.52-| .00 | 26.03-| 199.55-|MR D MCCARTHY |Pr#:558379 M/Aid:PR
I have resorted to grep the INVOICES and C/NOTES strings to separate files then doing the substitution on each file which works for the C/NOTES but still have the same issue with INVOICES where it keeps prepending the Cs where there are multiple transactions for the same debtor.
Am I on the correct path in using the combinations, just not using them correctly, or is there a better way to achieve this?
Any guidance would be greatly appreciated.
"C/NOTES " != "C/NOTES"
). Also, every time you find a match, you are editing the entire file and not just the line you want to edit.