21 exercises shell question: counting the number of digits

[1] How

Calculation documents a.txt number of digits in each row and appeared to calculate the entire document appeared in a total of several numbers

A.txt example as follows:

sdhhyh776dbbgbfg

dhhdffhhhs556644382

Operating results as follows:

3

9

sum=12

[Core] Points

sed to delete non-numeric characters, calculate the length

calculate the sum for loop

【script】

#!/bin/bash

# 可以逐行打印出
# 1. sed -n "$i"p
# 2. while read line; do echo $line; done < a.txt

sum=0

while read line
do
    line_n=`echo $line | sed s/[^0-9]//g | wc -L`
    echo $line_n
    sum=$[$sum+$line_n]
done < $1

echo "sum: $sum"

 

Guess you like

Origin www.cnblogs.com/dingzp/p/10991460.html