在Linux中用AWk编程:统计词频 统计考试成绩,假设学生成绩清单如下 编写shell脚本实现统计一个小组的成绩情况,统计信息包括:...
\u5728Linux\u4e2d\u7528AWK\u7f16\u7a0b\uff1a\u7edf\u8ba1\u8bcd\u9891 \u7edf\u8ba1\u8003\u8bd5\u6210\u7ee9\uff0c\u5047\u8bbe\u5b66\u751f\u6210\u7ee9\u6e05\u5355\u5982\u4e0b\uff1ay:root:/tmp/ss> cat xx44.datjasper: 80 82 84 84 88 92 andrea: 85 89 90 90 94 95 oellis: 89 90 92 96 96 98mona: 70 70 77 83 85 89john: 78 85 88 91 92 94dunce: 60 60 61 62 64 80y:root:/tmp/ss> ./xx44Total 6 Average 83jasper: 80 82 84 84 88 92 average 85(B)andrea: 85 89 90 90 94 95 average 90.5(A)oellis: 89 90 92 96 96 98 average 93.5(A)mona: 70 70 77 83 85 89 average 79(C)john: 78 85 88 91 92 94 average 88(B)dunce: 60 60 61 62 64 80 average 64.5(D)high: 4 low: 2A - 2B - 2C - 1D - 1y:root:/tmp/ss> cat xx44#!/bin/bashawk 'BEGIN{ sum=0}function getmark(s){ if(s=60 && s=70 && s=80 && s=90) mark="A" return mark}{ name[NR]=$1 subtotal[$1]=0 for(i=2;i= allav) hicount++ else locount++ markcnt[getmark(average[name[key]])]++ } printf("\n"); printf("\nhigh: %s low: %s\n", hicount, locount) for(key in markcnt) printf("%s - %s\n", key, markcnt[key])}' xx44.dat
#!/bin/bash
# 2013/07/01 sun
#\u529f\u80fd\u8bf4\u660e\uff1a\u5b9e\u73b0\u7edf\u8ba1\u5f55\u5165\u5b66\u751f\u6210\u7ee9\u7684\u603b\u4eba\u6570\uff0c\u5f97\u51fa\u6700\u9ad8\u5206\uff0c\u6700\u4f4e\u5206\uff0c\u5e73\u5747\u5206\uff0c\u65b9\u5dee\uff0c\u6807\u51c6\u65b9\u5dee\uff0c\u8fd8\u6709\u5404\u4e2a\u5206\u6570\u6bb5\u7684\u6240\u5360\u767e\u5206\u6bd4
echo "\u8bf7\u8f93\u5165\u6240\u6709\u5b66\u751f\u7684\u6210\u7ee9\uff0c\u4ee5\u7a7a\u683c\u9694\u5f00:"
read -a score_arr
#\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u6765\u5b58\u653e\u7528\u6237\u8f93\u5165\u7684\u6210\u7ee9
sorted_arr=($(printf '%s\n' "${score_arr[@]}"|sort -n -r))
#\u5f55\u5165\u5b66\u751f\u6210\u7ee9\uff0c\u5e76\u4e14\u662f\u6210\u7ee9\u4e4b\u95f4\u4f7f\u7528\u7a7a\u683c\u9694\u5f00
declare -i i b=0 c=0 d=0 e=0 f=0
#\u5b9a\u4e49\u53d8\u91cfi\u6765\u5faa\u73af\u6570\u7ec4\uff0c\u4f9d\u6b21\u8bfb\u51fa\u6570\u7ec4\u91cc\u9762\u7684\u6570\u636e\uff0c\u5b9a\u4e49\u4e94\u4e2a\u521d\u503c\u4e3a0\u7684\u53d8\u91cf\u6765\u5b58\u653e\u6bcf\u4e2a\u5206\u6570\u6bb5\u7684\u4eba\u6570
for (( i=0; $i<${#sorted_arr[@]}; i=$i+1 ))
#\u8fdb\u884cfor\u5faa\u73af\uff0c\u4f9d\u6b21\u53d6\u51fa\u6570\u7ec4\u91cc\u9762\u7684\u6570\u636e
do
#\u5bf9\u6bcf\u4e2a\u53d6\u51fa\u6765\u7684\u6570\u636e\u8fdb\u884c\u5224\u65ad
case ${sorted_arr[i]} in
?|?\.*|[1-5]?|[1-5]?\.*)
#\u5982\u679c\u53d6\u51fa\u6570\u7ec4\u91cc\u9762\u7684\u6570\u636e\u572860\u5206\u4ee5\u4e0b
b=$(($b+1));;
#\u53d8\u91cfb\u81ea\u52a01\uff0c\u8fdb\u884c\u7edf\u8ba1\u6570\u636e
6?|6?\.*)
#\u5982\u679c\u53d6\u51fa\u6570\u7ec4\u91cc\u9762\u7684\u6570\u636e\u572860\u5206--69\u5206\u4e4b\u95f4
c=$(($c+1));;
#\u53d8\u91cfc\u81ea\u52a01\uff0c\u8fdb\u884c\u7edf\u8ba1\u6570\u636e
7?|7?\.*)
#\u5982\u679c\u53d6\u51fa\u6570\u7ec4\u91cc\u9762\u7684\u6570\u636e\u572870\u5206--79\u5206\u4e4b\u95f4
d=$(($d+1));;
#\u53d8\u91cfd\u81ea\u52a01\uff0c\u8fdb\u884c\u7edf\u8ba1\u6570\u636e
8?|8?\.*)
#\u5982\u679c\u53d6\u51fa\u6570\u7ec4\u91cc\u9762\u7684\u6570\u636e\u572880\u5206--89\u5206\u4e4b\u95f4
e=$(($e+1));;
#\u53d8\u91cfe\u81ea\u52a01\uff0c\u8fdb\u884c\u7edf\u8ba1\u6570\u636e
100|100\.0|9?|9?\.*)
#\u5982\u679c\u53d6\u51fa\u6570\u7ec4\u91cc\u9762\u7684\u6570\u636e\u572890\u5206--100\u5206\u4e4b\u95f4
f=$(($f+1));;
#\u53d8\u91cff\u81ea\u52a01\uff0c\u8fdb\u884c\u7edf\u8ba1\u6570\u636e
*)
score_rank[i]="Not a Score";;
esac
done
#\u5224\u65ad\u7ed3\u675f
count=$(($b+$c+$d+$e+$f))
#\u7b97\u51fa\u603b\u4eba\u6570
declare -i j max min ave countall=0 u ua=0 ual=0 uall=0 fang
#\u5b9a\u4e49\u53d8\u91cfj\uff0c\u6700\u5927\u503cmax\uff0c\u6700\u5c0f\u503cmin\uff0c\u5e73\u5747\u6570ave\uff0c\u6240\u6709\u4eba\u7684\u5206\u6570\u4e4b\u548ccountall
max=${sorted_arr[0]}
#\u83b7\u53d6\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u6570\uff0c\u4e5f\u5c31\u662f\u6700\u5927\u6570
j=$((${#sorted_arr[@]}-1))
#\u5f97\u5230\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u7684\u6700\u540e\u7684\u4e00\u4e2a\u5206\u6570\u7684\u4e0b\u6807
min=${sorted_arr[j]}
#\u83b7\u53d6\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u6570\uff0c\u4e5f\u5c31\u662f\u6700\u5c0f\u6570
for (( j=0; $j<${#sorted_arr[@]}; j=$j+1 ))
#\u8fdb\u884cfor\u5faa\u73af\uff0c\u8ba9\u5b66\u751f\u6210\u7ee9\u76f8\u52a0\uff0c\u7b97\u51fa\u5b66\u751f\u6210\u7ee9\u7684\u603b\u5206
do
countall=$countall+${sorted_arr[j]}
done
ave=$countall/$count
#\u7b97\u51fa\u5e73\u5747\u6210\u7ee9
for (( j=0; $j<${#sorted_arr[@]}; j=$j+1 ))
#\u8fdb\u884cfor\u5faa\u73af\uff0c\u8ba9\u5b66\u751f\u6210\u7ee9\u76f8\u52a0\uff0c\u7b97\u51fa\u5b66\u751f\u6210\u7ee9\u7684\u603b\u5206
do
ua=$(awk -v va=${sorted_arr[j]} -v vb=$ave 'BEGIN{printf "%2d" ,va-vb}')
#\u8ba9\u6bcf\u4e00\u4e2a\u6570\u90fd\u51cf\u53bb\u5e73\u5747\u6570\u7136\u540e\u8d4b\u503c\u4e8eua
ual=$(awk -v va=$ua -v vb=$ua 'BEGIN{printf "%2d" ,va*vb}')
#\u7b97\u5f97ua\u7684\u5e73\u65b9
uall=$(awk -v va=$uall -v vb=$ual 'BEGIN{printf "%2d" ,va+vb}')
#\u7b97\u51fa\u5b83\u4eec\u7684\u603b\u5408\uff0c\u4e5f\u5c31\u662f\u65b9\u5dee
done
u=$(awk -v va=$uall -v vb=$count 'BEGIN{printf "%2d" ,va/vb}')
#\u7b97\u51fa\u5b66\u751f\u6210\u7ee9\u7684\u5e73\u5747\u5206\uff0c\u5e76\u8d4b\u503c\u4e8eave
#\u4e0b\u9762\u662f\u8ba1\u7b97\u6bcf\u4e2a\u5206\u6570\u6bb5\u7684\u767e\u5206\u6570
countb=$(awk -v va=$b -v vb=$count 'BEGIN{printf "%2.2f%%" ,va*100/vb}')
#echo $countb
countc=$(awk -v va=$c -v vb=$count 'BEGIN{printf "%2.2f%%" ,va*100/vb}')
#echo $countc
countd=$(awk -v va=$d -v vb=$count 'BEGIN{printf "%2.2f%%" ,va*100/vb}')
#echo $countd
counte=$(awk -v va=$e -v vb=$count 'BEGIN{printf "%2.2f%%" ,va*100/vb}')
#echo $counte
countf=$(awk -v va=$f -v vb=$count 'BEGIN{printf "%2.2f%%" ,va*100/vb}')
#echo $countf
#\u8f93\u51fa\u7ed3\u679c
echo "\u603b\u4eba\u6570 \u6700\u9ad8\u5206 \u6700\u4f4e\u5206 \u5e73\u5747\u5206 \u65b9\u5dee "
echo "$count $max $min $ave $u "
echo "\u6807\u51c6\u65b9\u5dee="
echo "scale=5;sqrt($u)" | bc
#\u7b97\u51fa\u6807\u51c6\u65b9\u5dee
echo " 60\u5206\u4ee5\u4e0b 60--69 70--79 80--89 90--100"
echo "\u4eba\u6570" " $b $c $d $e $f"
echo "\u6bd4\u4f8b" "$countb $countc $countd $counte $countf"
awk '{for ( a=2;a<=(NF+1);a++ ){sum=sum+$a}}{print $1,sum/(NF-1);sum=0}' /test/student.data | sort -k2 -r | awk 'BEGIN{A[1]="A";A[2]="B";A[3]="C";A[4]="D";A[5]="E";A[6]="F"}{print A[NR]":",$0}'
(2)每个科目的平均分(从上至下平均分分别是数据中从左至右科目的)
#!/bin/bash
for (( i=2;i<=(NR+1);i++ ))
do
awk '{sum+=$'$i'}END{print sum/NR}' /test/student.data
done
(2)-1班级总平均分
awk '{for ( a=2;a<=(NF+1);a++ ){sum=sum+$a}}END{print sum/(NF-1)}' student.data
#!/bin/awk -f
BEGIN{
FS="[: ]"
#保存学生数
count=0;
great=0;
less=0;
}
{
#student_average用于保存每个学生的平均分
student_average[$1]=($2+$3+$4+$5+$6+$7)/6
}
END{
#class_average保存班平均分
for (student in student_average)
{
print student": "student_average[student]
count++;
class_average+=student_average[student]*6
}
class_average/=count;
print "class average: "class_average
for(student in student_average)
{
if(student_average[student]*6>=class_average)
great++;
else
less++;
}
print "the count of student that greater than average: "great
print "the count of student that less than average: "less
}
我没写等级相关的统计,因为你没给出每个的等级的分数线
如何运行:
把代码保存为student.awk
chmod +x student.awk
./sutdent.awk student.data
student.data是你的数据文件
绛旓細Linux鏂囨湰澶勭悊绁炲櫒锛歛wk锛宻ed锛実rep绠浠婰inux鐜涓紝鏈変笁浣嶄笉鍙垨缂虹殑鏂囨湰澶勭悊涓撳锛屽畠浠槸awk锛宻ed锛屽拰grep锛屾瘡涓伐鍏烽兘鎷ユ湁鐙壒鐨勫姛鑳姐傞鍏堬紝awk鏄竴涓己澶х殑鏂囨湰澶勭悊璇█锛屼笓闀夸簬瀵规枃浠跺唴瀹硅繘琛屾繁鍏ュ垎鏋愩傚畠閫氳繃妯″紡鍖归厤鍜屽姩浣滄墽琛岋紝杞绘澗瀹炵幇鏁版嵁绛涢夊拰鏍煎紡杞崲銆備緥濡傦紝浣犲彲浠鐢╝wk蹇熸壘鍑烘枃鏈腑鐨鐗瑰畾...
绛旓細鎵鏈鐨勭紪绋涔︾睄閮芥槸閫氳繃涓涓淗ello world!鈥濈畝鍗曠▼搴忓叆闂紝浣滀负鍊熼壌锛屾湰鏂囦篃閲囩敤姝ゆ柟娉曞甫澶у鍏ラ棬銆傜幇鍦紝鍦ㄥ綋鍓嶇洰褰曚笅寤虹珛涓涓枃鏈枃浠秇ello.txt锛屽唴瀹瑰涓嬶細Hello world!鍦ㄥ懡浠よ涓緭鍏ヤ互涓嬪懡浠锛歛wk '{ print }' hello.txt 鎵ц鍚巋ello.txt鏂囦欢鐨勫唴瀹规樉绀哄湪灞忓箷涓銆傜紪鍐欏苟涓旀墽琛宎wk绋嬪簭鐨勬柟娉曞緢绠鍗...
绛旓細awk 鐨勫唴缃彉閲忋傚彉閲忓悕绉 浠h〃鐨勫惈涔 NF 姣忎竴琛岋紙$0锛夋嫢鏈夌殑瀛楁鎬绘暟 NR 褰撳墠 awk 鎵澶勭悊鐨勬槸 鈥滅鍑犺鈥 鏁版嵁 FS 褰撳墠鍒嗛殧绗︼紝榛樿绌烘牸閿 awk 鐨勯昏緫杩愮畻绗 鐗圭偣 AWK鏄竴绉嶄紭鑹殑鏂囨湰澶勭悊宸ュ叿銆傚畠涓嶄粎鏄 Linux 涓涔熸槸浠讳綍鐜涓幇鏈夌殑鍔熻兘鏈寮哄ぇ鐨勬暟鎹鐞嗗紩鎿庝箣涓銆傝繖绉缂栫▼鍙婃暟鎹搷浣滆瑷锛堝叾...
绛旓細Linux鏄竴绉嶉潪甯稿彈娆㈣繋鐨勬搷浣滅郴缁燂紝涓庤澶氬紑鍙戣呴兘浼浣跨敤瀹冩潵鎾板啓鑴氭湰浠ュ畬鎴愮壒瀹氫换鍔°傛墍浠ワ紝瀛︿細杩愮敤Linux鑴氭湰璋冭瘯绁炲櫒鍙互鏋佸ぇ鍦版柟渚挎垜浠皟璇曠▼搴忋傛垜浠皢浠嬬粛25涓鍦↙inux涓鏈甯哥敤鐨勫懡浠わ紝姣斿`ls`銆乣grep`銆乣sed`鍜宍awk`绛夈1. `ls`鍛戒护锛 涓昏鐢ㄤ簬鍒楀嚭鏂囦欢鎴栫洰褰曠殑鍚嶇О銆備緥濡傦紝浣跨敤`ls`鍛戒护鍙互鏌ョ湅...
绛旓細linux涓璉Pa鍜孖Plink鏈夊暐鍖哄埆锛焛pa琛ㄧずipaddress(show)鐩稿綋浜庢樉绀篒P鍦板潃淇℃伅锛屽亸鍚戜簬涓婂眰銆俰plink(show)琛ㄧず閾捐矾灞傜殑淇℃伅锛屾洿搴曞眰锛屽亸鍚戜簬鐗╃悊灞傦紝濡備綘鍙互璁剧疆缃戝崱鐨剈pdown.閭d箞灏辨槸iplinksetdownethX,iplinksetupethX銆傛垜鍦╨inux閲岀敤鍛戒护鍑烘潵isadirectory鏄庝箞鍥炰簨锛熶綘浣跨敤鐨勫懡浠ゅ簲璇ユ槸閽堝鏂囦欢鐨勫懡浠わ紝鍦...
绛旓細鍏跺疄FS="'"'"'";鐨鎰忔濇槸浠 ' 锛堝崟鍐掑彿锛変綔涓哄垎闅旂锛堢矘璐村埌杩欎笂闈㈢湅鐫涓嶆槸澶竻妤氾紝浣犺嚜宸卞紕涓嬶級linux-hg2:~ # echo '_"'"'"'"_'_"'"_ (涓や釜鍙屽啋鍙凤紝涓棿涓涓崟鍐掑彿锛塴inux-hg2:~ # cat test.txt ab'c'b linux-hg2:~ # awk 'BEGIN { FS="'"'"'";RS="\n"}{print $...
绛旓細GNU Awk 鏄竴涓己澶х殑鏂囨湰澶勭悊宸ュ叿锛岃兘鎵ц鎺掑簭绛変换鍔★紝瓒呰秺浜嗗叾浠栧父瑙佸伐鍏凤紝濡俿ort銆傚畠涓嶄粎鏄懡浠よ宸ュ叿锛屾洿鍍忎竴绉缂栫▼璇█锛屽叿鏈夌伒娲荤殑缂栫▼鑳藉姏銆傛湰鏂囦富瑕佸叧娉℅NU Awk锛屽敖绠″瓨鍦ㄥ涓増鏈紝浣嗗湪GNU+Linux绯荤粺涓锛岄粯璁浣跨敤鐨勫氨鏄痝awk锛岃屽湪娌℃湁瀹夎鐨勬儏鍐典笅锛屽彲鑳介渶瑕佹墜鍔ㄦ寚瀹氫负gawk銆侫wk鐨勫己澶т箣澶勫湪浜庡叾...
绛旓細srand( [Expr] ) 灏 rand 鍑芥暟鐨绉嶅瓙鍊艰缃负 Expr 鍙傛暟鐨勫,鎴栧鏋滅渷鐣 Expr 鍙傛暟鍒浣跨敤鏌愬ぉ鐨勬椂闂淬傝繑鍥炲厛鍓嶇殑绉嶅瓙鍊笺 澶嶅埗浠g爜 浠g爜濡備笅: 涓句緥璇存槑: [chengmo@centos5 ~]$ awk 'BEGIN{OFMT="%.3f";fs=sin(1);fe=exp(10);fl=log(10);fi=int(3.1415);print fs,fe,fl,fi;}' 0.841 ...
绛旓細NR==2灏辨槸瀵圭浜岃鎵ц鍚庨潰鐨鎿嶄綔锛屼篃灏辨槸 {print $0}锛 鑰宲rint $0 鐨勬剰鎬濆氨鏄墦鍗板綋鍓嶈锛屼篃灏辨槸绗簩琛屻傛墍浠 awk 'NR==n{print $0}' file 杩欐潯鍛戒护鐨勪綔鐢ㄥ氨鏄墦鍗 file 鏂囦欢鐨勭n琛屻
绛旓細awk鍛戒护 awk鏄竴绉缂栫▼璇█锛岀敤浜鍦╨inux/unix涓嬪鏂囨湰鍜屾暟鎹繘琛屽鐞嗐傛暟鎹彲浠ユ潵鑷爣鍑嗚緭鍏(stdin)銆佷竴涓垨澶氫釜鏂囦欢锛屾垨鍏跺畠鍛戒护鐨勮緭鍑恒傚畠鏀寔鐢ㄦ埛鑷畾涔夊嚱鏁板拰鍔ㄦ佹鍒欒〃杈惧紡绛夊厛杩涘姛鑳斤紝鏄痩inux/unix涓嬬殑涓涓己澶х紪绋嬪伐鍏枫傚畠鍦ㄥ懡浠よ涓娇鐢锛屼絾鏇村鏄綔涓鸿剼鏈潵浣跨敤銆俛wk鏈夊緢澶氬唴寤虹殑鍔熻兘锛屾瘮濡傛暟缁...