我将以下数据存储在文件中:
number1:0 number2=1 minnumber2number3:1 minnumber3:0 minnumber4:0 maxnumber2number3:1 maxnumber3:0 maxnumber4:0 no. of number5 per bucket:35 num:
number1:0 number2=0 minnumber2number3:3 minnumber3:3 minnumber4:11577215 maxnumber2number3:3 maxnumber3:3 maxnumber4:14707215 no. of number5 per bucket:6 num:
number1:0 number2=1 minnumber2number3:1 minnumber3:0 minnumber4:0 maxnumber2number3:3 maxnumber3:1 maxnumber4:1882188 no. of number5 per bucket:8 num:
number1:0 number2=1 minnumber2number3:2 minnumber3:248 minnumber4:10000000 maxnumber2number3:2 maxnumber3:248 maxnumber4:10000000 no. of number5 per bucket:91 num:
number1:0 number2=1 minnumber2number3:2 minnumber3:239 minnumber4:11641499 maxnumber2number3:2 maxnumber3:244 maxnumber4:2248700 no. of number5 per bucket:8999 num:
number1:0 number2=1 minnumber2number3:2 minnumber3:251 minnumber4:2000000 maxnumber2number3:2 maxnumber3:251 maxnumber4:2000000 no. of number5 per bucket:8986 num:
number1:0 number2=1 minnumber2number3:2 minnumber3:232 minnumber4:14775115 maxnumber2number3:1 maxnumber3:0 maxnumber4:0 no. of number5 per bucket:6385 num:
number1:0 number2=1 minnumber2number3:2 minnumber3:253 minnumber4:9404695 maxnumber2number3:1 maxnumber3:0 maxnumber4:0 no. of number5 per bucket:8863 num:
number1:0 number2=1 minnumber2number3:3 minnumber3:3 minnumber4:14777215 maxnumber2number3:1 maxnumber3:0 maxnumber4:0 no. of number5 per bucket:2679 num:
现在我需要在“每桶数量为5号”之后检索所有数字并将它们相加。例如,我需要总结以下内容:
35
6
8
91
8999
8986
6385
8863
2679
我知道我可以使用像python这样的编程语言来做同样的事情,但我正在学习linux命令是否有像grep这样的linux命令,这可以帮助我做同样的事情。
答案 0 :(得分:1)
如果您对按列号选择列感到满意:
tr ":" " " <table | awk '{print $21}'
假设您的数据位于文件table
中,则第一个命令将:
转换为空格,第二个命令转到第21列。
编辑:纯awk
,指定字段分隔符。
awk 'BEGIN{FS="[ :]"} {print $21}' table
第二次编辑:选择grep
并与awk
求和:
grep -oE "no\. of number5 per bucket:[^ ]*" table | awk 'BEGIN{FS=":"} {S+=$2} END{print S}'
第3次编辑:在阅读了Anton的解决方案后,我被提醒,您还可以使用sed
来匹配名称并获取组中的解决方案(\1
)
sed -rn 's/^.*no\. of number5 per bucket:([0-9]*).*/\1/p' table | awk '{S+=$1} END {print S}'
答案 1 :(得分:1)
您可以尝试使用sed:
sed -e 's/^.*bucket://' | sed -e 's/[a-zA-Z]*:$//'
答案 2 :(得分:1)
一个简单的bash脚本也可以:
#!/bin/bash
declare -i sum=0
while read -r line; do
n=${line##*number5 per bucket:}
sum+=$((${n% num:}))
done <"$1"
printf "\n the sum is: %d\n\n" $sum
<强>输出强>
$ bash script.sh filename
the sum is: 36052