当时第二题不会连接数据库 给忘了
2 使用shell脚本实现将mysql的test.u表中的id和name去重统计,并在shell中判断统计的数据是否正确,正确请输出打印id个数和"id数据统计正确",不正确请输出name个数和"name数据统计不正确"。(15分)
create table test.u(
id int(8),
name varchar(22)
);
数据为:
insert into test.u(id,name) values(1,"goudan");
insert into test.u(id,name) values(1,"zs");
insert into test.u(id,name) values(2,"laotie");
insert into test.u(id,name) values(1,"laotie");
insert into test.u(id,name) values(1,"laotie");
shell中id的判断值为2(判断要求相等),shell中的name总计统计值为2(判断要求不等)。
结果:
id=2,id数据统计正确
name=3,name数据统计不正确
#!/bin/bash
sum_name=3
sum_id=2
res_id=$(mysql -uroot -proot -e "use test;select count(distinct(id)) as id from test.u;" | awk '{print $1}' )
## 结果是 id 2
res_id2=$(echo ${res_id} | awk '{print $2}')
## 结果是 2 然后再比较
echo ${res_id2} | awk '{print "id="$0;if($0==2){print "id数据统计正确"}else{print "id数据统计不正确"}}'
res_name=$(mysql -uroot -proot -e "use test;select count(distinct(name)) as name from test.u;" | awk '{print $1}')
res_name2=$(echo ${res_name1} | awk '{print $2}')
echo ${res_name2} | awk '{print "name="$0;if($0==2){print "name数据统计正确"}else{print "name数据统计不正确"}}'
[root@hadoop01 shell]# vi test1.sh
[root@hadoop01 shell]# /bin/bash ./test1.sh
[root@hadoop01 shell]# /bin/bash ./test1.sh
id=2
id数据统计正确
name=
name数据统计不正确
[root@hadoop01 shell]#
1 使用shell脚本将linux服务器的eth0网卡中配置的ip地址截取出来即可(15分)
[root@hadoop01 ~]# ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | awk -F ':' '{print $2}'
192.168.37.111
[root@hadoop01 ~]# vi /home/shellgetIp
[root@hadoop01 ~]#
[root@hadoop01 ~]# sh /home/shellgetIp
192.168.37.111
[root@hadoop01 ~]#
3 使用awk实现如下需求(总计15分,每小题3分)
这个比较麻烦 但是还能做出来
数据:
名字 语文 数学 英语
lh 92 68 70
zyt 94 88 75
ls 96 78 78
hgw 90 70 56
yxx 80 88 73
hz 90 98 70
xyd 60 88 73
hj 90 58 70
cs 50 58 11
1、算每个人的平均成绩?
[root@hadoop01 shell]# cat data.txt | awk 'BEGIN {print "姓名","\t avg"} {print $1,"\t",($2+$3+$4)/3} '
姓名 avg
lh 76.6667
zyt 85.6667
ls 84
hgw 72
yxx 80.3333
hz 86
xyd 73.6667
hj 72.6667
cs 39.6667
[root@hadoop01 shell]# vi /31.sh
[root@hadoop01 shell]# sh /31.sh
2、每个学科的平均成绩?
[root@hadoop01 shell]# cat data.txt | awk 'BEGIN {print "语文","\t\t数学","\t\t英语"}{sum1+=$2}{sum2+=$3}{sum3+=$4}END{print sum1/NR,"\t"sum2/NR,"\t"sum3/NR}'
语文 数学 英语
82.4444 77.1111 64
[root@hadoop01 shell]# vi /32.sh
[root@hadoop01 shell]# sh /32.sh
语文 数学 英语
82.4444 77.1111 64
[root@hadoop01 shell]#
3、总均每个分数段的人数以及百分比??
[root@hadoop01 shell]# sh /33.sh
<60 1 11.1111 %
60-70 0 %
70-80 4 44.4444 %
80-90 4 44.4444 %
90-100 0 %
[root@hadoop01 shell]# vi /33.sh
[root@hadoop01 shell]#
#/bin/bash
cat data.txt | awk '{print ($2+$3+$4)/3}' | awk '{if($1 < 60){sum60+=1};\
if($1 <70 && $1 >=60){sum70+=1};\
if($1 <80 && $1 >=70){sum80+=1};\
if($1 <90 && $1 >=80){sum90+=1};\
if($1 <=100 && $1 >=90){sum100+=1} };\
END{\
print "<60 "," "sum60," ",(sum60/NR)*100,"%";\
print "60-70 ",sum70," ",(sum70/NR)*100,"%";\
print "70-80 ",sum80," ",(sum80/NR)*100,"%";\
print "80-90 ",sum90," ",(sum90/NR)*100,"%";\
print "90-100 ",sum100," ",(sum100/NR)*100,"%"}'
5、统计成材率?每一门成绩都大于60分的人数/总人数
成材率 80%(数据不一定对)
[root@hadoop01 shell]# vi ./35.sh
[root@hadoop01 shell]# sh ./35.sh
成材率为: 55.5556 %
[root@hadoop01 shell]#
#!/bin/bash
cat data.txt | awk '\
{if($2>60 && $3 > 60 && $4 > 60){sum+=1}};END{print "成材率为:",sum/NR*100,"%"}'