我正尝试添加到我的Awk代码(如下)中,以将每年的平均FX汇率添加到我的数据中(如果仅提供部分年份的信息,则为每部分年的平均FX汇率)。 有关示例(请参见输出文件的最后两行)(带有不准确的样本号数据)。
缺少的部分是如何获取正确行的平均值。关于如何使用Awk计算此信息的任何想法?
输入文件
remove1
MMM YYYY USD/GBP CAD/GBP EUR/GBP JPY/GBP CHF/GBP AUD/GBP HKD/GBP NZD/GBP KRW/GBP MXN/GBP
Jan 2017 1.2348 1.6288 1.1611 141.81 1.2441 1.6537 9.5778 1.7318 1457.1 26.447
Feb 2017 1.2494 1.6380 1.1732 141.08 1.2508 1.6304 9.6953 1.7283 1426.1 25.369
Mar 2017 1.2346 1.6528 1.1549 139.39 1.2365 1.6199 9.5882 1.7626 1399.4 23.802
Apr 2017 1.2644 1.7001 1.1797 139.31 1.2653 1.6791 9.8297 1.8151 1435.4 23.750
May 2017 1.2920 1.7580 1.1698 145.03 1.2752 1.7379 10.0605 1.8594 1455.2 24.247
Jun 2017 1.2806 1.7030 1.1399 142.05 1.2396 1.6940 9.9874 1.7714 1449.3 23.225
Jul 2017 1.2995 1.6488 1.1272 146.03 1.2480 1.6652 10.1480 1.7658 1471.9 23.137
Aug 2017 1.2951 1.6324 1.0963 142.18 1.2499 1.6362 10.1299 1.7740 1465.3 23.052
Sep 2017 1.3338 1.6383 1.1198 147.83 1.2843 1.6735 10.4205 1.8381 1511.3 23.785
Oct 2017 1.3200 1.6638 1.1229 149.08 1.2966 1.6953 10.3033 1.8758 1494.6 24.849
Nov 2017 1.3230 1.6893 1.1262 149.16 1.3113 1.7358 10.3270 1.9211 1455.8 25.020
Dec 2017 1.3404 1.7118 1.1328 151.38 1.3234 1.7540 10.4727 1.9256 1453.7 25.667
Jan 2018 1.3825 1.7180 1.1328 153.24 1.3276 1.7374 10.8108 1.9027 1474.9 26.137
Feb 2018 1.3962 1.7571 1.1312 150.76 1.3059 1.7748 10.9210 1.9116 1507.0 26.037
Mar 2018 1.3973 1.8070 1.1329 148.25 1.3243 1.8003 10.9564 1.9256 1497.2 26.016
Apr 2018 1.4077 1.7922 1.1468 151.49 1.3630 1.8321 11.0477 1.9432 1504.6 25.874
May 2018 1.3466 1.7335 1.1396 147.62 1.3427 1.7898 10.5694 1.9367 1450.5 26.298
Jun 2018 1.3287 1.7443 1.1380 146.26 1.3154 1.7732 10.4268 1.9147 1455.1 26.977
Jul 2018 1.3170 1.7292 1.1267 146.83 1.3098 1.7783 10.3352 1.9390 1479.2 24.969
Aug 2018 1.2849 1.6791 1.1182 142.59 1.2771 1.7520 10.0874 1.9298 1446.0 24.191
remove2
需要输出
YYYY/MM|MMM YYYY|MMM|YYYY|USD/GBP|CAD/GBP|EUR/GBP|JPY/GBP|CHF/GBP|AUD/GBP|HKD/GBP|NZD/GBP|KRW/GBP|MXN/GBP||||||||
2017/01|1 Jan 2017|Jan|2017|1.2348|1.6288|1.1611|141.81|1.2441|1.6537|9.5778|1.7318|1457.1|26.447||||||||
2017/02|1 Feb 2017|Feb|2017|1.2494|1.6380|1.1732|141.08|1.2508|1.6304|9.6953|1.7283|1426.1|25.369||||||||
2017/03|1 Mar 2017|Mar|2017|1.2346|1.6528|1.1549|139.39|1.2365|1.6199|9.5882|1.7626|1399.4|23.802||||||||
2017/04|1 Apr 2017|Apr|2017|1.2644|1.7001|1.1797|139.31|1.2653|1.6791|9.8297|1.8151|1435.4|23.750||||||||
2017/05|1 May 2017|May|2017|1.2920|1.7580|1.1698|145.03|1.2752|1.7379|10.0605|1.8594|1455.2|24.247||||||||
2017/06|1 Jun 2017|Jun|2017|1.2806|1.7030|1.1399|142.05|1.2396|1.6940|9.9874|1.7714|1449.3|23.225||||||||
2017/07|1 Jul 2017|Jul|2017|1.2995|1.6488|1.1272|146.03|1.2480|1.6652|10.1480|1.7658|1471.9|23.137||||||||
2017/08|1 Aug 2017|Aug|2017|1.2951|1.6324|1.0963|142.18|1.2499|1.6362|10.1299|1.7740|1465.3|23.052||||||||
2017/09|1 Sep 2017|Sep|2017|1.3338|1.6383|1.1198|147.83|1.2843|1.6735|10.4205|1.8381|1511.3|23.785||||||||
2017/10|1 Oct 2017|Oct|2017|1.3200|1.6638|1.1229|149.08|1.2966|1.6953|10.3033|1.8758|1494.6|24.849||||||||
2017/11|1 Nov 2017|Nov|2017|1.3230|1.6893|1.1262|149.16|1.3113|1.7358|10.3270|1.9211|1455.8|25.020||||||||
2017/12|1 Dec 2017|Dec|2017|1.3404|1.7118|1.1328|151.38|1.3234|1.7540|10.4727|1.9256|1453.7|25.667||||||||
2018/01|1 Jan 2018|Jan|2018|1.3825|1.7180|1.1328|153.24|1.3276|1.7374|10.8108|1.9027|1474.9|26.137||||||||
2018/02|1 Feb 2018|Feb|2018|1.3962|1.7571|1.1312|150.76|1.3059|1.7748|10.9210|1.9116|1507.0|26.037||||||||
2018/03|1 Mar 2018|Mar|2018|1.3973|1.8070|1.1329|148.25|1.3243|1.8003|10.9564|1.9256|1497.2|26.016||||||||
2018/04|1 Apr 2018|Apr|2018|1.4077|1.7922|1.1468|151.49|1.3630|1.8321|11.0477|1.9432|1504.6|25.874||||||||
2018/05|1 May 2018|May|2018|1.3466|1.7335|1.1396|147.62|1.3427|1.7898|10.5694|1.9367|1450.5|26.298||||||||
2018/06|1 Jun 2018|Jun|2018|1.3287|1.7443|1.1380|146.26|1.3154|1.7732|10.4268|1.9147|1455.1|26.977||||||||
2018/07|1 Jul 2018|Jul|2018|1.3170|1.7292|1.1267|146.83|1.3098|1.7783|10.3352|1.9390|1479.2|24.969||||||||
2018/08|1 Aug 2018|Aug|2018|1.2849|1.6791|1.1182|142.59|1.2771|1.7520|10.0874|1.9298|1446.0|24.191||||||||
2017/99||AVG|2017|1.3404|1.7118|1.1328|151.38|1.3234|1.7540|10.4727|1.9256|1453.7|25.667||||||||
2018/99||AVG|2018|1.3825|1.7180|1.1328|153.24|1.3276|1.7374|10.8108|1.9027|1474.9|26.137||||||||
代码尝试(部分有效)
awk ' BEGIN { OFS="|" }
{ if ($1 ~ /Jan/) $21="01" }
{ if ($1 ~ /Feb/) $21="02" }
{ if ($1 ~ /Mar/) $21="03" }
{ if ($1 ~ /Apr/) $21="04" }
{ if ($1 ~ /May/) $21="05" }
{ if ($1 ~ /Jun/) $21="06" }
{ if ($1 ~ /Jul/) $21="07" }
{ if ($1 ~ /Aug/) $21="08" }
{ if ($1 ~ /Sep/) $21="09" }
{ if ($1 ~ /Oct/) $21="10" }
{ if ($1 ~ /Nov/) $21="11" }
{ if ($1 ~ /Dec/) $21="12" }
{ if ($1 ~ /MMM/) $21="MM" }
###################
{ if ( !/remove1| remove2/ ) { ( MMM_YYYY = 1" "$1" "$2 ) ( YYYY_MM = $2"/"$21 ); print YYYY_MM, MMM_YYYY, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20 } }
' fxdata >fxdata_Processed.txt
awk ' { sub ( /1 MMM YYYY/, "MMM YYYY" ) }1
' fxdata_Processed.txt >fxdata_Processed_2.txt
答案 0 :(得分:0)
这是第一个fx速率的原型,您可以添加另一个并修复格式...
$ awk -v OFS='|' '/^[A-Z][a-z][a-z]/ {sum[$2]+=$3; count[$2]++}
NF>1 && $1=$1;
END {for(k in sum) print k"/99","AVG",k,sum[k]/count[k]}' file
MMM|YYYY|USD/GBP|CAD/GBP|EUR/GBP|JPY/GBP|CHF/GBP|AUD/GBP|HKD/GBP|NZD/GBP|KRW/GBP|MXN/GBP
Jan|2017|1.2348|1.6288|1.1611|141.81|1.2441|1.6537|9.5778|1.7318|1457.1|26.447
Feb|2017|1.2494|1.6380|1.1732|141.08|1.2508|1.6304|9.6953|1.7283|1426.1|25.369
Mar|2017|1.2346|1.6528|1.1549|139.39|1.2365|1.6199|9.5882|1.7626|1399.4|23.802
Apr|2017|1.2644|1.7001|1.1797|139.31|1.2653|1.6791|9.8297|1.8151|1435.4|23.750
May|2017|1.2920|1.7580|1.1698|145.03|1.2752|1.7379|10.0605|1.8594|1455.2|24.247
Jun|2017|1.2806|1.7030|1.1399|142.05|1.2396|1.6940|9.9874|1.7714|1449.3|23.225
Jul|2017|1.2995|1.6488|1.1272|146.03|1.2480|1.6652|10.1480|1.7658|1471.9|23.137
Aug|2017|1.2951|1.6324|1.0963|142.18|1.2499|1.6362|10.1299|1.7740|1465.3|23.052
Sep|2017|1.3338|1.6383|1.1198|147.83|1.2843|1.6735|10.4205|1.8381|1511.3|23.785
Oct|2017|1.3200|1.6638|1.1229|149.08|1.2966|1.6953|10.3033|1.8758|1494.6|24.849
Nov|2017|1.3230|1.6893|1.1262|149.16|1.3113|1.7358|10.3270|1.9211|1455.8|25.020
Dec|2017|1.3404|1.7118|1.1328|151.38|1.3234|1.7540|10.4727|1.9256|1453.7|25.667
Jan|2018|1.3825|1.7180|1.1328|153.24|1.3276|1.7374|10.8108|1.9027|1474.9|26.137
Feb|2018|1.3962|1.7571|1.1312|150.76|1.3059|1.7748|10.9210|1.9116|1507.0|26.037
Mar|2018|1.3973|1.8070|1.1329|148.25|1.3243|1.8003|10.9564|1.9256|1497.2|26.016
Apr|2018|1.4077|1.7922|1.1468|151.49|1.3630|1.8321|11.0477|1.9432|1504.6|25.874
May|2018|1.3466|1.7335|1.1396|147.62|1.3427|1.7898|10.5694|1.9367|1450.5|26.298
Jun|2018|1.3287|1.7443|1.1380|146.26|1.3154|1.7732|10.4268|1.9147|1455.1|26.977
Jul|2018|1.3170|1.7292|1.1267|146.83|1.3098|1.7783|10.3352|1.9390|1479.2|24.969
Aug|2018|1.2849|1.6791|1.1182|142.59|1.2771|1.7520|10.0874|1.9298|1446.0|24.191
2017/99|AVG|2017|1.28897
2018/99|AVG|2018|1.35761
请注意,您的平均计算似乎不正确。对于我计算的列,您仅使用了2017年末和2018年末。
也许您可能需要添加一个更好的模式,也许是/Jan|Feb|Mar|.../
而不是我的快捷方式。