我有以下名為 bank_scpt.txt 的 bash 腳本:
#!/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
這個腳本背后的想法是連接兩個銀行賬戶 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 檔案。
uj5u.com熱心網友回復:
由于某些欄位wp.txt用雙引號括起來,我假設以開頭的欄位2degree是相同的。(盡管您提供的wp.txt不幸錯過了2degree.的關鍵行。)然后$3~r您的 awk 腳本中的條件正在"2degree"
針對^2degree失敗的模式進行測驗。
然后修改該行:
wp_fc="^2degr"
類似于:
wp_fc="^\"2degr"
那么它將起作用。
作為旁注:
- 始終建議發布一致的輸入檔案集、腳本、結果和預期結果。您提供的輸入檔案與您最初發布的輸出完全無關,我們無法重現該問題。
- 您最好避免將
txt后綴放在可執行腳本檔案中。它有效,但令人困惑。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/407364.html
標籤:
