in

Passing Bash shell varialbes in AWK. One regex works the other does not


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 of 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”?



Source: https://stackoverflow.com/questions/70604616/passing-bash-shell-varialbes-in-awk-one-regex-works-the-other-does-not

Scribe — Track and share your educational journey

Xiaomi 11i HyperCharge, Xiaomi 11i Launched in India, Check Price and Specifications