对特定列值求和,直到达到某个值
Sum specific column value until a certain value is reached
我想打印第一列的值,直到它达到某个值,例如;
43 12.00 53888
29 10.00 36507
14 9.00 18365
8 8.00 10244
1 7.00 2079
1 9.50 1633
0 6.00 760
我希望输出为:
val = 90
43 12.00 53888
29 10.00 36507
14 9.00 18365
Perl 来拯救!
perl -sape ' $s += $F[0] ; exit if $s > $vv' -- -vv=90 file
-s
允许从命令行设置变量,-vv=90
将 $vv
变量设置为 90
-p
逐行处理输入,处理后打印每一行
-a
在空白处拆分每一行并填充 @F 数组
变量 $s 用于保存 运行 总和。只有当总和小于$vv时才打印该行,一旦总和太大,程序退出。
您能否尝试使用显示的示例进行以下、编写和测试。当第 1 列的总和大于提到的值时,明确将 exit
置于条件中,以避免不必要地读取 Input_file.
的其余部分
awk -v val="90" '(+prev)>val{exit} (+prev)<=val{print}{prev+=}' Input_file
或
awk -v val="90" '(+prev)>val{exit} (+prev)<=val; {prev+=}' Input_file
解释:为以上添加详细解释。
awk -v val="90" ' ##Starting awk program from here and mentioning variable val as 90 here.
(+prev)>val{ ##Checking condition if first field and prev variable sum is greater than val then do following.
exit ##exit from program to save some time.
}
(+prev)<=val; ##Checking condition if sum of and prev is lesser than or equal to val then print the current line.
{
prev+= ##keep adding 1st field to prev variable here.
}
' Input_file ##Mentioning Input_file name here.
考虑小的一行awk
awk -v val=85 '{ s += ; if ( s <= val ) print }'
甚至
awk -v val=85 '{ s+= } s <= val'
考虑一个更小的 awk,它非常符合
awk -v v=90 '((v-=)<0){exit}1' file
或最小的:
awk -v v=90 '0<=(v-=)' file
我想打印第一列的值,直到它达到某个值,例如;
43 12.00 53888
29 10.00 36507
14 9.00 18365
8 8.00 10244
1 7.00 2079
1 9.50 1633
0 6.00 760
我希望输出为:
val = 90
43 12.00 53888
29 10.00 36507
14 9.00 18365
Perl 来拯救!
perl -sape ' $s += $F[0] ; exit if $s > $vv' -- -vv=90 file
-s
允许从命令行设置变量,-vv=90
将$vv
变量设置为 90-p
逐行处理输入,处理后打印每一行-a
在空白处拆分每一行并填充 @F 数组
变量 $s 用于保存 运行 总和。只有当总和小于$vv时才打印该行,一旦总和太大,程序退出。
您能否尝试使用显示的示例进行以下、编写和测试。当第 1 列的总和大于提到的值时,明确将 exit
置于条件中,以避免不必要地读取 Input_file.
awk -v val="90" '(+prev)>val{exit} (+prev)<=val{print}{prev+=}' Input_file
或
awk -v val="90" '(+prev)>val{exit} (+prev)<=val; {prev+=}' Input_file
解释:为以上添加详细解释。
awk -v val="90" ' ##Starting awk program from here and mentioning variable val as 90 here.
(+prev)>val{ ##Checking condition if first field and prev variable sum is greater than val then do following.
exit ##exit from program to save some time.
}
(+prev)<=val; ##Checking condition if sum of and prev is lesser than or equal to val then print the current line.
{
prev+= ##keep adding 1st field to prev variable here.
}
' Input_file ##Mentioning Input_file name here.
考虑小的一行awk
awk -v val=85 '{ s += ; if ( s <= val ) print }'
甚至
awk -v val=85 '{ s+= } s <= val'
考虑一个更小的 awk,它非常符合
awk -v v=90 '((v-=)<0){exit}1' file
或最小的:
awk -v v=90 '0<=(v-=)' file