bash “for循环”下的awk“if条件”中的总和变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21812074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum variable in awk 'if condition' under 'for loop'
提问by rohit
I have a file with 3 columns (n=3) and FS = " " (file1.txt):
我有一个包含 3 列 (n=3) 和 FS = " " (file1.txt) 的文件:
cat file1.txt
3.1 6.6 0
2.4 7.1 4.9
5.7 1.2 6.1
Here, I would like to give an 'if condition' in awk
for each column, where it would test for a condition and return a value of 1 or 0 (depending on the result) and store it in another variable, such as:
在这里,我想awk
为每一列提供一个“if 条件” ,它会在其中测试条件并返回 1 或 0 的值(取决于结果)并将其存储在另一个变量中,例如:
#!/bin/bash
#code1
awk '{
if ( != 0)
{ x1 = 1 }
else
{ x1 = 0}
if ( != 0)
{ x2 = 1 }
else
{ x2 = 0}
if ( != 0)
{ x3 = 1 }
else
{ x3 = 0}
x = x1 + x2 + x3
print x;
}' file1.txt > output.txt
The desired output in this case would be:
在这种情况下所需的输出是:
cat output.txt
2
3
3
Which I am getting without any problem.
我得到的没有任何问题。
Consider the case in which instead of having 3 columns, there were say 10 columns (n=10) and the same if
condition is to be applied to each column. In this case I would like to run a for
loop, under which the same if
condition is to be defined. But I think I am making an error in specifying the nth field. Also how to perform the sum of the xn variables (x1 + X2 + X3...X10). Here is what I have tried so far:
考虑这样一种情况,其中有 10 列(n=10)而不是 3 列,并且if
对每一列应用相同的条件。在这种情况下,我想运行一个for
循环,在该循环下if
定义相同的条件。但我认为我在指定第 n 个字段时犯了一个错误。还有如何执行 xn 变量的总和 (x1 + X2 + X3...X10)。这是我迄今为止尝试过的:
awk '{
for (n=1; n<=10; n++)
if ($n != 0)
{ xn = 1 }
else
{ xn = 0 }
xn+=xn
print xn;
}' file1_10fields.txt > output_10fields.txt
This is not giving me the correct output. Where am I making the mistake? Is there a more elegant way to do this?
Are xn+=xn
and print xn
supposed to be outside the loop? Also, what is the correct way to get the sum?
这没有给我正确的输出。我在哪里犯了错误?有没有更优雅的方法来做到这一点?是否xn+=xn
并且print xn
应该在循环之外?另外,获得总和的正确方法是什么?
采纳答案by anubhava
You can use this awk:
你可以使用这个awk:
awk '{sum=0; for (i=1; i<=NF; i++){sum += $i ? 1 : 0} print sum}' file
2
3
3
- Read more about ternary operator
Awk has conditional operator i.e ternary operator( ?: ) whose feature is similar to the awk If Else Statement. If the conditional-expression is true, action1 will be performed and if the conditional-expression is false action2 will be performed.
- 阅读有关三元运算符的更多信息
awk 有条件运算符,即三元运算符( ?: ),其功能类似于 awk If Else 语句。如果条件表达式为真,将执行 action1,如果条件表达式为假,将执行 action2。
回答by Ed Morton
This is probably what you're expecting to see:
这可能是您期望看到的:
$ awk '
{
sum = 0
for (i=1; i<=NF; i++) {
sum += ($i == 0 : 0 : 1)
# or `sum += ($i ? 1 : 0)`
# or `sum += ($i != 0)`
}
print sum
}
' file
2
3
3
but you could alternatively do this with GNU awk for \<
...\>
word-boundaries:
但是您也可以使用 GNU awk 来为\<
...\>
字边界执行此操作:
$ gawk '{ print gsub(/\<[^0][^ ]*\>/,1) }' file
2
3
3
If you like the loop, then you should accept @anubhava's answer, but do add the parens around the ternary expression so it'll work on all awks, and it makes the code a little clearer.
如果你喜欢这个循环,那么你应该接受@anubhava 的回答,但一定要在三元表达式周围添加括号,这样它就可以在所有 awk 上工作,并且它使代码更清晰一些。