我承诺给大家的,要连续发出shell脚本练习题,我做到了。你能每个脚本都去动手写写吗?我希望你也能做到!
今日练习题: 计算文档a.txt中每一行中出现的数字个数并且要计算一下整个文档中一共出现了几个数字。例如a.txt内容如下:
12aa*lkjskdj
alskdflkskdjflkjj
我们脚本名字为 ncount.sh, 运行它时:
bash ncount.sh a.txt
输入结果应该为:
2
0
sum:2
如果你使用while循环,不要写成这样的形式:cat a.txt |while read line ; do .... ;done
应该是这样的形式:
while read line ; do ... ;done
今日练习题: 计算文档a.txt中每一行中出现的数字个数并且要计算一下整个文档中一共出现了几个数字。例如a.txt内容如下:
12aa*lkjskdj
alskdflkskdjflkjj
我们脚本名字为 ncount.sh, 运行它时:
bash ncount.sh a.txt
输入结果应该为:
2
0
sum:2
如果你使用while循环,不要写成这样的形式:cat a.txt |while read line ; do .... ;done
应该是这样的形式:
while read line ; do ... ;done
0
本帖最后由 陈沛 于 2014-9-22 08:40 编辑
#/bin/bash
# 2014-09-21
# chenpei
fileName=$1
#判读文件是否存在
if [ ! -f $fileName ]
then
exit;
fi
#每行出现数字次数
for i in `cat $fileName`
do
echo $[ `echo $i | wc -m`-`echo $i | sed 's#[0-9]##g' | wc -m` ]
done
#文档中数字总共出现次数
echo -e "sum:" $[`cat $fileName | wc -m`-`cat $fileName | sed 's#[0-9]##g' | wc -m`]
#/bin/bash
# 2014-09-21
# chenpei
fileName=$1
#判读文件是否存在
if [ ! -f $fileName ]
then
exit;
fi
#每行出现数字次数
for i in `cat $fileName`
do
echo $[ `echo $i | wc -m`-`echo $i | sed 's#[0-9]##g' | wc -m` ]
done
#文档中数字总共出现次数
echo -e "sum:" $[`cat $fileName | wc -m`-`cat $fileName | sed 's#[0-9]##g' | wc -m`]
0
- #!/bin/bash
- #ww
- sum=0
- filename=$1
- if [ -f $filename ]
- then
- >.count
- lines=`cat $filename|wc -l`
- for i in `seq 1 $lines`
- do
- count=`sed 's/[^0-9]//g' $filename | sed -n "$i"'p'|wc -m`
-
- c=`echo "$count-1"|bc`
- echo $c
- sum=$[$c+$sum]
- done
- # cat .count
- echo "sum:$sum"
- else
- echo "$filename is not a file"
- echo "sh ncount.sh filename"
- fi
0
本帖最后由 zyfeifie 于 2014-9-22 10:26 编辑
#!/bin/bash
sum=0
if [ ! -f $1 ];
then
echo "the file $1 is not exists."
exit
fi
for i in `cat $1`
do
num=`echo -n $i | sed 's/[^0-9]//g' |wc -m`
sum=$[$sum+$num]
echo $num
done
echo "sum: $sum"
#!/bin/bash
sum=0
if [ ! -f $1 ];
then
echo "the file $1 is not exists."
exit
fi
for i in `cat $1`
do
num=`echo -n $i | sed 's/[^0-9]//g' |wc -m`
sum=$[$sum+$num]
echo $num
done
echo "sum: $sum"
0
- #!/bin/bash
- ## This script is for echo length of number in a file every row and count.
- ## Written by Louis at 2014/09/22 10:15
- sum=0
- for i in `cat a.txt`; do
- num=`echo $i|grep -o '[0-9]*'`
- len=`expr length "$num"`
- echo $len
- sum=$[$sum+$len]
- done
- echo "sum:"$sum
0
#!/bin/bash
#file=$1
if [ ! -f $1 ] || [ ! -s $1 ]
then
echo "Please input a exists and isn't empty file!"
exit
fi
n=`awk 'END {print NR}' $1`
row=1
sum=0
while [ $row -le $n ]
do
c=`sed -n "$[row]"p $1 |grep -o '[0-9]' |wc -l`
row=$[$row+1]
sum=$[$sum+$c]
echo $c
done
echo "sum:$sum"
#file=$1
if [ ! -f $1 ] || [ ! -s $1 ]
then
echo "Please input a exists and isn't empty file!"
exit
fi
n=`awk 'END {print NR}' $1`
row=1
sum=0
while [ $row -le $n ]
do
c=`sed -n "$[row]"p $1 |grep -o '[0-9]' |wc -l`
row=$[$row+1]
sum=$[$sum+$c]
echo $c
done
echo "sum:$sum"
0
本帖最后由 齐天大圣 于 2014-9-22 12:18 编辑
- #!/bin/bash
- #The num sum of each line.
- #2014-09-22.
- file="$1"
- n=`wc -l $file |cut -d' ' -f1`
- for i in `seq 1 $n`
- do
- m=`sed -n "$i"p $file |sed 's/[^0-9]//g' |wc -c`
- m1=$[$m-1]
- echo $m1>>/root/sum.txt
- done
- sum=`cat sum.txt |xargs |sed 's/ /+/g' |bc`
- echo "sum:$sum">>sum.txt
- cat /root/sum.txt
- rm -f /root/sum.txt
0
本帖最后由 ocean 于 2014-9-22 12:53 编辑
{:5_121:}
- [root@www ~]# cat 0922.bash
- #!/bin/bash
- # Desc: Count number of each line, and count sum.
- # Writen by Jeffery.Su
- # Date: Sep,22 2014
- function NUM_COUNT () {
- sed s/[^0-9]//g $1 > $1.tmp
- awk '{print "line:"NR,"\t"length($1)}' $1.tmp | tee $1.tmp2
- awk 'BEGIN{sum=0}{sum+=$2}END{printf "%s %d\n" ,"sum:" ,sum}' $1.tmp2
- rm -rf $1.tmp*
- }
- NUM_COUNT $1
0
#!/bin/bash
sun=0
sed 's/[^0-9]//g' $1 > result.txt
while read row
do
echo ${row} > temp.dat
a=`wc -c temp.dat | awk '{print $1-1}'`
echo $a
sun=$[${sun}+$a]
done < result.txt
echo "sum:${sun}"
sun=0
sed 's/[^0-9]//g' $1 > result.txt
while read row
do
echo ${row} > temp.dat
a=`wc -c temp.dat | awk '{print $1-1}'`
echo $a
sun=$[${sun}+$a]
done < result.txt
echo "sum:${sun}"
0
本帖最后由 程城 于 2014-9-22 17:07 编辑
#!/bin/bash
#written by chengcheng
sum=0
for i in `cat a.txt|xargs`;do
a=`echo $i |wc -m`
b=`echo $i |sed 's/[0-9]//g'|wc -m`
c=$[$a-$b]
echo $c
sum=$[$c+$sum]
done
echo $sum
结果:#sh 0922.sh
2
0
2
#!/bin/bash
#written by chengcheng
sum=0
for i in `cat a.txt|xargs`;do
a=`echo $i |wc -m`
b=`echo $i |sed 's/[0-9]//g'|wc -m`
c=$[$a-$b]
echo $c
sum=$[$c+$sum]
done
echo $sum
结果:#sh 0922.sh
2
0
2
0
#! /bin/bash
while read line ;do
a=`echo $line|sed 's/[^0-9]//g'|wc -c`
echo $a
done <$1
sum=0
for i in `cat -A $1`;do
n=`echo -n $i|sed 's/[^0-9]//g'|wc -c`
sum=$[$sum+$n]
done
echo "sum:$sum"
while read line ;do
a=`echo $line|sed 's/[^0-9]//g'|wc -c`
echo $a
done <$1
sum=0
for i in `cat -A $1`;do
n=`echo -n $i|sed 's/[^0-9]//g'|wc -c`
sum=$[$sum+$n]
done
echo "sum:$sum"
0
- #!/bin/bash
- #
- if [ ! -f $1 ] ;then
- echo "file is not exist"
- exit
- else
- Line=`cat /root/$1|wc -l`
- for I in `seq 1 $Line`;do
- num=`sed -n "$I"p $1|sed 's#[^0-9]##g'`
- len=`expr length "$num"`
- echo $len
- let sum+=$len
- done
- echo $sum
- fi
0
本帖最后由 butterfly梧桐雨 于 2014-10-10 15:31 编辑
只想出来一部分,先回帖瞧一些答案
[yunzhuw@APT_SH-01 ~/workspace/shell_practice/aming]$ sed 's/[^0-9]//g' a.txt
12
==========================
参考了老师的答案以后,如下:
#!/bin/bash
#practice
sum=0
for fline in `cat $1`
do
i=`echo -n $fline |sed 's/[^0-9]//g'| wc -m`
echo $i
sum=$[$sum+$i]
done
echo 'sum:'$sum
==========================
一些感想:
原本以为很简单的,不就数个数么,后来发现远非自己想的那么简单,很多东西时间长了有些忘记,各种感慨啊
1)cat以后再用for可以逐个读取行,又没想起来,打板子
2)sed,wc命令报错说不是文件或文件夹时,可以用echo,继续打板子
3)echo -n 去除行位换行符的统计
4)先读取每行后再进行sed,wc,对于统计结果为0的行很有效,老师英明啊
5)有其他同学先判断文件是否存在的,特别好,有专业范儿,学习了
谢谢大家,share了这么好的东西,尤其阿铭老师,后面的作业一定补上
只想出来一部分,先回帖瞧一些答案
[yunzhuw@APT_SH-01 ~/workspace/shell_practice/aming]$ sed 's/[^0-9]//g' a.txt
12
==========================
参考了老师的答案以后,如下:
#!/bin/bash
#practice
sum=0
for fline in `cat $1`
do
i=`echo -n $fline |sed 's/[^0-9]//g'| wc -m`
echo $i
sum=$[$sum+$i]
done
echo 'sum:'$sum
==========================
一些感想:
原本以为很简单的,不就数个数么,后来发现远非自己想的那么简单,很多东西时间长了有些忘记,各种感慨啊
1)cat以后再用for可以逐个读取行,又没想起来,打板子
2)sed,wc命令报错说不是文件或文件夹时,可以用echo,继续打板子
3)echo -n 去除行位换行符的统计
4)先读取每行后再进行sed,wc,对于统计结果为0的行很有效,老师英明啊
5)有其他同学先判断文件是否存在的,特别好,有专业范儿,学习了
谢谢大家,share了这么好的东西,尤其阿铭老师,后面的作业一定补上
0
#/bin/bash
#writen by alex on 2014.11.22 about findnum
a=0
for i in `cat /test/nap.txt`;do
sum=`echo $i|sed 's#[^0-9]##g'|wc -m`
echo $sum
a=$[$a+$sum]
done
echo "全文数字总计为:"$a
#/bin/bash
#writen by alex on 2014.11.22 about findnum
a=0
for i in `cat /test/nap.txt`;do
sum=`echo $i|sed 's#[^0-9]##g'|wc -m`
echo $sum
a=$[$a+$sum]
done
echo "全文数字总计为:"$a
0
本帖最后由 sss 于 2015-6-12 15:13 编辑
- #!/bin.bash
- #Ian statistics document a few figures
- #2015/05/19
- sum=0
- for i in `cat $1`
- do
- num=`echo -n $i | sed 's/[^0-9]//g' | wc -c`
- echo $num
- ((sum=$sum+$num))
- done
- echo "sum:"$sum
0
#!/bin/bash
#The script was Wriiten by Xusj at 20150728.
sum=0
for i in `cat -A a.txt`
do
n=`echo -n $i|sed 's/[^0-9]//g'|wc -c`
echo $n
sum=$[$sum+$n]
done
echo "sum:"$sum
#The script was Wriiten by Xusj at 20150728.
sum=0
for i in `cat -A a.txt`
do
n=`echo -n $i|sed 's/[^0-9]//g'|wc -c`
echo $n
sum=$[$sum+$n]
done
echo "sum:"$sum
0
while read line do echo "$line" > m.txt c=`sed -r 's/[^0-9]//g' m.txt|wc -m` let b=$c-1 echo "$b " let sum+=$b done
0
- #/bin/bash
- if [ ! -f $1 && -z $1 ]
- then
- exit 0
- fi
- for i in `cat $1|sed 's/[^0-9]//g'`
- do
- echo $[ `echo $i | wc -m` ]
- done
- echo "sum:" $[`cat $1 | sed 's/[^0-9]//g'| wc -m`]
0
本帖最后由 蓝色风暴 于 2015-12-24 15:30 编辑
#!/bin/bash
sum=0
sed 's/[^0-9]//g' /etc/passwd > /tmp/1.txt
for i in `cat /tmp/1.txt`
do
num=`echo "$i" | wc -m`
echo "$[ $num-1 ]"
sum=$[ $sum+$num-1 ]
done
echo "$sum"
#!/bin/bash
sum=0
sed 's/[^0-9]//g' /etc/passwd > /tmp/1.txt
for i in `cat /tmp/1.txt`
do
num=`echo "$i" | wc -m`
echo "$[ $num-1 ]"
sum=$[ $sum+$num-1 ]
done
echo "$sum"
0
#!/bin/bash
file=/etc/passwd
c=`cat /etc/passwd |wc -l`
sum=0
for i in `seq 1 $c`;do
n=`sed -n "$i"p $file |sed 's/[^0-9]//g'`
l=`expr length $n`
sum=$[$sum+$l]
echo $l
done
echo "sum:$sum"
file=/etc/passwd
c=`cat /etc/passwd |wc -l`
sum=0
for i in `seq 1 $c`;do
n=`sed -n "$i"p $file |sed 's/[^0-9]//g'`
l=`expr length $n`
sum=$[$sum+$l]
echo $l
done
echo "sum:$sum"
0
关键是cat -A wc -c 无法识别数字
#!/bin/bash
sum=0
sumcount=0
sed -i.bak 's/[^0-9]/ /g' $1
n=`wc -l $1 |awk '{print $1}'`
for i in `seq 1 $n`
do
sumcount=`echo $i|wc -c `
echo "count in this line: $sumcount"
sum=$[$sum+$sumcount]
done
echo $sum
#!/bin/bash
sum=0
sumcount=0
sed -i.bak 's/[^0-9]/ /g' $1
n=`wc -l $1 |awk '{print $1}'`
for i in `seq 1 $n`
do
sumcount=`echo $i|wc -c `
echo "count in this line: $sumcount"
sum=$[$sum+$sumcount]
done
echo $sum
0
- #!/bin/bash
- FILE=$1
- m=0
- for i in `cat $FILE`;do
- NUM=`echo $i|sed 's/[a-Z]//g'|sed 's/\W//g'|wc -m`
- let NUM=NUM-1
- #因为每一行都有个$,所以需要减一
- echo $NUM
- let m=m+NUM
- done
- echo $m
0
- #!/bin/bash
- ## 计算每行有几个数字,统计总共有多少数字
- sum=0
- while read LINE; do
- tmp=$((`echo $LINE |sed 's/[^0-9]//g' |wc -m` - 1))
- echo $tmp
- let sum+=tmp
- done < a.txt
- echo "sum: $sum"
0
#!/bin/bash
while read line
do
echo $line|grep -o "[0-9]"
done<1.txt
echo -e "sum: `grep -o "[0-9]" 1.txt|wc -l`"
while read line
do
echo $line|grep -o "[0-9]"
done<1.txt
echo -e "sum: `grep -o "[0-9]" 1.txt|wc -l`"
0
#! /bin/bash
sum=0
for i in `cat -A a.txt`
do
n=`echo -n $i |sed 's/[^0-9]//g' |wc -c`
echo $n
sum=$[$sum+$n]
done
echo "sum: "$sum
sum=0
for i in `cat -A a.txt`
do
n=`echo -n $i |sed 's/[^0-9]//g' |wc -c`
echo $n
sum=$[$sum+$n]
done
echo "sum: "$sum
0
#!/bin/bash
##作业要求,计算某个文本每行出现有几个数字。
##并计算出数字出现的总和。
## 以passwd 文件为例
## 用法:sh a.sh /etc/passwd
sum=0
#定义sum的结果为0
exec < $1
#在当前的shell中读入$1的内容
while read line
do
number=`echo $line|sed 's/[^0-9]//g'|wc -L`
#读取每行的数字
echo $number
sum=$[$sum+$number]
#将结果相加
done
echo $sum
##作业要求,计算某个文本每行出现有几个数字。
##并计算出数字出现的总和。
## 以passwd 文件为例
## 用法:sh a.sh /etc/passwd
sum=0
#定义sum的结果为0
exec < $1
#在当前的shell中读入$1的内容
while read line
do
number=`echo $line|sed 's/[^0-9]//g'|wc -L`
#读取每行的数字
echo $number
sum=$[$sum+$number]
#将结果相加
done
echo $sum
0
测试通过,仅供参考。
- #!/bin/bash
- count=0
- sum=0
- while read line
- do
- count=`echo $line| grep -o '[0-9]' |wc -l `
- echo "count: $count"
- let "sum +=count"
- done < $1
- echo "sum: $sum"
测试通过,仅供参考。
编辑回复