与if

时间:2015-07-04 20:24:33

标签: unix awk

我试图将字段$ 2乘以.75或.1

我有这些数据:

Disputed,279
Processed,12112
Uncollected NSF,4732
Declined,14
Invalid / Closed Account,3022

Awk声明:

#!/usr/local/bin/gawk -f

BEGIN { FPAT="([^,]*)|(\"[^\"]+\")"; FS=OFS=","; OFMT="%.2f"; }

{
        if ($1  "/Disputed|Uncollected|Invalid/")
                $3 = $2 * .75
        else 
                if ($1 ~  "/Processed|Declined/")
                $3 = $2 * 0.10
        print
}

预期产出:

Disputed,279,209.25
Processed,12112,1211.2
Uncollected NSF,4732,3549
Declined,14,1.4
Invalid / Closed Account,3022,2266.5

目前的结果:

Disputed,279,209.25
Processed,12112,9084
Uncollected NSF,4732,3549
Declined,14,10.5
Invalid / Closed Account,3022,2266.5

这些乘以.75:争议,未收集的NSF和无效/已关闭的帐户

这些乘以.1:已处理和已拒绝

是什么导致所有记录乘以.75?

编辑:这是我的工作解决方案......

 #!/usr/local/bin/gawk -f

BEGIN {
     FPAT="([^,]*)|(\"[^\"]+\")"
     FS=OFS=","
     OFMT="%.2f"
     print "status","acct type","count","amount"
}

NF>1 {
     $4=$3 * ($1 ~ /Processed|Declined/ ? 0.10 : 0.75 )
     print
     trans+=$3
     fee+=$4
}

END {
     printf "------------\n"
     print "# of transactions: " trans
     print "processing fee: " fee
}

是的,有四个字段。 2美元是一个隐藏的特殊领域!

status,acct type,count,amount
Processed,Savings,502,50.2
Uncollected NSF,Checking,4299,3224.25
Disputed,Checking,263,197.25
Processed,Checking,11610,1161
Uncollected NSF,Savings,433,324.75
Declined,Checking,14,1.4
Invalid / Closed Account,Checking,2868,2151
Disputed,Savings,16,12
Invalid / Closed Account,Savings,154,115.5
------------
# of transactions: 20159
processing fee: 7237.35

3 个答案:

答案 0 :(得分:2)

问题

您缺少匹配的运算符~。这句话:

 if ($1  "/Disputed|Uncollected|Invalid/")

始终求值为true,因为它会检查$1"/Disputed|Uncollected|Invalid/"的串联是否为空 - 并且它不是。

尝试改为:

if ($1 ~ /Disputed|Uncollected|Invalid/)

<强>实施例

您可以使用以下单行来查看此行为:

$ awk 'BEGIN { if ("" "a") { print "true" } else { print "false" }}'
true
$ awk 'BEGIN { if ("" "") { print "true" } else { print "false" }}'
false
$ awk 'BEGIN { if ("") { print "true" } else { print "false" }}'
false
$ awk 'BEGIN { if (RS FS "a") { print "true" } else { print "false" }}'
true
$ awk 'BEGIN { if (variable) { print "true" } else { print "false" }}'
false
$ awk 'BEGIN { var="0"; if (var) { print "true" } else { print "false" }}'
true

答案 1 :(得分:2)

正如另一张海报所说,你在第一个正则表达式之前省略了private WebView webView; @Override protected void onCreate(Bundle savedInstanceState){ // ... this.webView = (WebView) findViewById(R.id.webView); // ... } @Override public void onBackPressed(){ // now you don't need findViewById because you can access field prepared in onCreate if (webView.canGoBack()) { webView.goBack(); } else { super.onBackPressed(); } } 运算符。

此外,不要在正则表达式的开头和结尾包含斜杠。将正则表达式用斜杠(如Perl / Ruby / JavaScript)或引号括起来 - 不是两者都包含在内。

~

答案 2 :(得分:2)

在awk中编写代码的方法是使用三元表达式,例如:

$ awk 'BEGIN{FS=OFS=","} {print $0, $2 * ($1 ~ /Processed|Declined/ ? 0.10 : 0.75)}' file
Disputed,279,209.25
Processed,12112,1211.2
Uncollected NSF,4732,3549
Declined,14,1.4
Invalid / Closed Account,3022,2266.5

请注意,regexp常量由/分隔(请参阅http://www.gnu.org/software/gawk/manual/gawk.html#Regexp),但是awk可以从变量和/或字符串常量构造动态regexp(请参阅http://www.gnu.org/software/gawk/manual/gawk.html#Computed-Regexps),所以当你写道: / p>

"/Processed|Declined/"

在适合动态正则表达式($1 ~ <regexp>)的上下文中,awk构造了一个正则表达式:

`/Processed` OR `Declined/`

(注意文字/字符作为正则表达式术语的一部分)而不是您想要的内容:

`Processed` OR `Declined`

你可以在这里看到这种效果:

$ echo 'abc' | awk '$0 ~ /b|x/'
abc
$ echo 'abc' | awk '$0 ~ "/b|x/"'
$ echo 'a/bc' | awk '$0 ~ "/b|x/"'
a/bc

现在,看看你是否可以解决这个问题:

$ echo 'abc' | awk '$0 ~ "/b|x/"'
$ echo 'abc' | awk '"/b|x/"'
abc

即。为什么第一个没有打印,只有第二个打印输入。