I have the following bash script called bank_scpt.txt:
#!/bin/bash
anz="$1"
wp="$2"
# anz fixed cost search patterns:
anz_fc="^Aver"
# wp fixed cost search patterns:
wp_fc="2degrees"
# Preperation to get anz file ready for concatenation.
anz="$(awk -v r="$anz_fc" 'BEGIN{FS=OFS="\t"} NR>1 {split($7,a,"/"); print a[3]"-"a[2]"-"a[1], $6, $2, $3, $4, "az" OFS ($6 > 0 ? "vi" : $2~r ? "fc" : "vc")}' "$anz" | column -s $'\t' -t)"
# Preperation to get wp file ready for concatenation.
wp="$(awk -v r="$wp_fc" 'BEGIN{FS="," ; OFS="\t"} NR>1 && $3~r {gsub(/"/, "", $0) ; split($1,a,"/"); print a[3]"-"a[2]"-"a[1], $2, $3, $4, $5, "wp", "fc"}' "$wp" | column -s $'\t' -t)"
echo "$anz" "$wp" |head -n 4
echo "$anz" "$wp" |tail -n 4
The idea behind this script is to concatenate two bank account txt files: anz.txt and wp.txt
When I run:
./bank_scpt.txt anz.txt wp.txt
I get the following desired output (Please note az and wp in column six indicate the bank text files the records come from az = anz.txt and wp = wp.txt):
2021-03-31 -8.50 Monthly A/C Fee az vc
2021-03-31 -250.00 Rutherford & Bond 4835******** 8848 C az vc
2021-03-31 -131.60 Avery Johnson Avery Johnso 592315 az fc
2021-03-31 50.00 Collins Tf 127 Driver Crescent az vi
2020-12-29 -71.50 2degrees Mobile Ltd DIRECT DEBIT 2365653 wp fc
2021-01-27 -70.00 2degrees Mobile Ltd DIRECT DEBIT 2365653 wp fc
2021-02-26 -70.00 2degrees Mobile Ltd DIRECT DEBIT 2365653 wp fc
2021-03-26 -70.00 2degrees Mobile Ltd DIRECT DEBIT 2365653 wp fc
However when I use a regex such as wp_fc="^2degr" I get the following output (the wp.txt file is completely ignored):
2021-03-31 -8.50 Monthly A/C Fee az vc
2021-03-31 -250.00 Rutherford & Bond 4835******** 8848 C az vc
2021-03-31 -131.60 Avery Johnson Avery Johnso 592315 az fc
2021-03-31 50.00 Collins Tf 127 Driver Crescent az vi
2020-04-09 64.40 Body Corporate Batchelor 1010 & 1036 az vi
2020-04-09 17.25 A D & C H Bailey Aron Bailey az vi
2020-04-06 46.00 Jm Lymburn 13 Thornley Titahi az vi
2020-04-02 17.25 A D & C H Bailey Aron Bailey az vi
My question is why am I able to use anz_fc="^Aver" but not wp_fc="^2degr"? And how can I change the second awk command so I can indeed use wp_fc="^2degr"?
I include here and excerpt of the original files:
head -n 5 anz.txt
Type Details Particulars Code Reference Amount Date ForeignCurrencyAmount ConversionCharge
Bank Fee Monthly A/C Fee -8.50 31/03/2021
Eft-Pos Rutherford & Bond 4835******** 8848 C 210331123119 -250.00 31/03/2021
Payment Avery Johnson Avery Johnso 592315 Labour -131.60 31/03/2021
Bill Payment Collins Tf 127 Driver Crescent I1600 50.00 31/03/2021
head -n 5 wp.txt
Date,Amount,Other Party,Description,Reference,Particulars,Analysis Code
01/04/2020,478.26,"ACC","Salary",,"ACC WKLY CMP","TO 02Apr2020"
02/04/2020,-7.50,"Edorne Labog","AUTOMATIC PAYMENT",,"Christian","Netflix"
02/04/2020,-150.00,"Christian rent cover","AUTOMATIC PAYMENT",,"146 Coromand",
26/03/2021,-70.00,"2degrees Mobile Ltd","DIRECT DEBIT","2365653",,"10009701292"
Please note that wp.txt is a csv file that I saved as a txt file.
CodePudding user response:
As some fields of wp.txt are enclosed with double quotes, I assume
the field which starts with 2degree will be the same. (Although your
provided wp.txt unfortunately misses the crutial lines of 2degree.)
Then the condition $3~r in your awk script is testing "2degree"
against the pattern ^2degree which fails.
Then modify the line:
wp_fc="^2degr"
to something like:
wp_fc="^\"2degr"
then it will work.
As side notes:
- It is always recommended to post the consistent set of input file(s), your script, the result, and your expected result. Your provided input files are not related with your initially posted output at all and we cannot reproduce the problem.
- You better to avoid putting the
txtsuffix to the executable script file. It works, but is confusing.
