#/bin/bash linecount=0 sum=0 count=0 while read line do for (( i=0; i<${#line};i++ )) do if [[ ${line:$i:1} =~ [1-5] ]] then count=$(($count+1)) fi done linecount=$(($linecount+1)) echo "line$linecount number:$count" sum=$(($sum+$count)) count=0 done<nowcoder.txt echo "sum is $sum" ##################################### coutline=0 count=0 sum=0 while read line do for((i=0;i<${#line};i++));do if [[ ${line:$i:1} =~ [1-5] ]];then let count=count+1 fi done let coutline=coutline+1 echo "line${coutline} number: ${count}" let sum=sum+count count=0 done<./nowcoder.txt echo "sum is ${sum}" ##################################### #!/bin/bash declare -A m line=1 while read l do len=${#l} for((i=0;i<$len;i++)) do if [[ ${l:$i:1} =~ [1-5] ]];then ((m[$line]++)) fi done if [[ -z ${m[$line]} ]];then m[$line]=0 fi ((line++)) done < nowcoder.txt sum=0 for i in ${!m[@]} do echo "line${i} number: ${m[$i]}" sum=$[$sum+${m[$i]}] done echo "sum is $sum" ##################################### awk '{ gsub(/[^1-5]/,"",$0); print "line"NR" number: "length($0); sum+=length($0); } END{print "sum is "sum}' ##################################### awk '{ gsub(/[^1-5]/,"",$0); print "line"NR" number: "length($0); sum+=length($0); } END{ print "sum is "sum }' nowcoder.txt