话说同事发给我一道shell 编程题,要求根据原始数据统计出每个qq号绑定哪些手机号及几个手机号,原始数据如下(qq号:手机号):
11235334:13443253456
11235335:13443253457
11235336:13443253458
11235333:13443253458
11235336:13443253459
11235334:13443253452
下面是我写的shell:
#!/bin/bash
line=$(cat hb | awk -F: '{print $1}' | sort | uniq |wc -l)
for ((i=1;i<=$line;i=i+1))
do
line1=$(cat hb | awk -F: '{print $1}' | sort | uniq|sed -n ''$i'p')
echo "-----------"
echo [$line1]
echo " "
grep $line1 hb|awk -F: '{print $2}'
echo " "
grep $line1 hb|awk -F: '{print $2}'| wc -l
echo " "
done
执行结果:
-----------
[11235333]
13443253458
1
-----------
[11235334]
13443253456
13443253452
2
-----------
[11235335]
13443253457
1
-----------
[11235336]
13443253458
13443253459
2