在 AWK 中传递 Bash shell 变量。一个正则表达式有效,另一个无效

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

我有以下名为 bank_scpt.txt 的 bash 脚本:

#!/bin/bash
  

anz=""
wp=""

# 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(,a,"/"); print a[3]"-"a[2]"-"a[1], , , , , "az" OFS ( > 0 ? "vi" : ~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 && ~r {gsub(/"/, "", [=12=]) ; split(,a,"/"); print a[3]"-"a[2]"-"a[1], , , , , "wp", "fc"}' "$wp" | column -s $'\t' -t)"

echo "$anz" "$wp" |head -n 4
echo "$anz" "$wp" |tail -n 4

此脚本背后的想法是连接两个银行帐户 txt 文件:anz.txt 和 wp.txt

当我运行:

./bank_scpt.txt anz.txt wp.txt

我得到以下所需的输出(请注意第六列中的 az 和 wp 表示记录来自 az = anz.txt 和 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

然而,当我使用 wp_fc="^2degr" 等正则表达式时,我得到以下输出(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-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 

我的问题是为什么我可以使用 anz_fc="^Aver" 而不能使用 wp_fc="^2degr"?我怎样才能更改第二个 awk 命令,以便我确实可以使用 wp_fc="^2degr"?

我在这里包含原始文件的摘录:

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"

请注意,wp.txt 是我保存为 txt 文件的 csv 文件。

由于 wp.txt 的某些字段用双引号括起来,我假设 以 2degree 开头的字段将相同。 (虽然你的 不幸的是 wp.txt 错过了 2degree 的关键线。) 那么你的 awk 脚本中的条件 ~r 正在测试 "2degree" 针对失败的模式 ^2degree
然后修改一行:

wp_fc="^2degr"

类似于:

wp_fc="^\"2degr"

那就可以了。

旁注:

  • 始终建议 post 一致的输入文件集, 您的脚本、结果和您的预期结果。您提供的 输入文件与您最初 posted 的输出完全无关 我们无法重现该问题。
  • 您最好避免将 txt 后缀添加到可执行脚本文件中。 它有效,但令人困惑。