LinuxÒÁµéÔ°ÂÛ̳'s Archiver

kkfly ·¢±íÓÚ 2006-4-19 20:00

ÈõÎÊÒ»¸öshell±à³ÌÌâ

¼ÙÉèscore.txtÎļþÖб£´æÁËÈý¸ö°à¼¶µÄѧÉú¿¼ÊԳɼ¨£¬Çëдһ¸öshell³ÌÐò¼ÆËãÿ¸ö°à¼¶µÄѧÉúÈËÊýÓëÆ½¾ù·Ö
¼ÙÉè ÎļþÖб£´æµÄÄÚÈݸñʽΪ   
°à¼¶                                       ÐÕÃû                                  ³É¼¨
01£¯02£¯03                           ÕÅÈý                                   98

ÎÒÓм¸¸ö²»Ã÷°×µÄµØ·½  ÔõôÑù½«Æ¥ÅäµÄ×Ö·ûת»¯ÎªÊý×Ö¸³Öµ¸øÒ»¸ö±äÁ¿ÓÃÀ´¼ÆËã
¸÷λ´óÏÀÓпհïÎÒдһ¸ö  ÎÒ³õѧshell  ÏëºÃºÃ²Î¿¼
лл

xlink ·¢±íÓÚ 2006-4-19 22:20

²»Óñà³Ì£¬Ò»¾äawk¾ÍÄܽâ¾ö¡£

¼ÈÈ»ÊÇÁ·Ï°Ì⣬Äã×Ô¼ºÔõô²»ÏÈ×öһϣ¿
×ö²»ÏÂÈ¥ÔÙÎÊ£¬²»ÊÇ»á¸üºÃÒ»µã£¿

Óв»Ã÷°×µÄ£¬¾ÍÒ»¸öÒ»¸öÎÊ£¬±ðÈ˰ïÄãдÁË´ð°¸Ò²Ã»Óã¬ÓÖ²»ÊÇ¿¼ÊÔ¡£

ljsky ·¢±íÓÚ 2006-4-19 22:42

ÔÚÊéÉÏÓÐÏàËÆµÄÀý×Ó£¬ºÃÏóÊÇÇóõÌÈ­µÀ¶ÎÊýµÄͳ¼Æ

ljsky@lab:~$ cat score.awk
#!/bin/awk -f
BEGIN{
class["01"]
class["02"]
class["03"]
score["01"]
score["02"]
score["03"]
}
{
for( index1 in class)
{
if($1 == index1 )
{
class[index1]++
score[index1] += $3
}
}
}
END{
for( index2 in class)
{
print "class " index2 " has students: " \
class[index2] " & average score is : " score[index2]/class[index2]
}
}

ljsky@lab:~$ cat score.txt
01 st1 100
02 st2 100
01 st3 90
02 st4 90
03 st5 80
01 st6 80
ljsky@lab:~$ awk -f score.awk score.txt
class 01 has students: 3 & average score is : 90
class 02 has students: 2 & average score is : 95
class 03 has students: 1 & average score is : 80
ljsky@lab:~$

kkfly ·¢±íÓÚ 2006-4-20 01:18

[QUOTE=xlink]²»Óñà³Ì£¬Ò»¾äawk¾ÍÄܽâ¾ö¡£

¼ÈÈ»ÊÇÁ·Ï°Ì⣬Äã×Ô¼ºÔõô²»ÏÈ×öһϣ¿
×ö²»ÏÂÈ¥ÔÙÎÊ£¬²»ÊÇ»á¸üºÃÒ»µã£¿

Óв»Ã÷°×µÄ£¬¾ÍÒ»¸öÒ»¸öÎÊ£¬±ðÈ˰ïÄãдÁË´ð°¸Ò²Ã»Óã¬ÓÖ²»ÊÇ¿¼ÊÔ¡£[/QUOTE]
ÎÒÃǸÕѧϰshell±à³Ì ²»ÖªµÀÓÃʲôÃüÁî ²»ÖªµÀawk ÃüÁî  ÎÒ×Ô¼ºÒ²×öÁËÒ»¶Îʱ¼ä ֻд³öÀ´Ò»°ë ËùÒÔ²ÅÀ´Îʵà  
ллÄãµÄÌáÐÑ  £º£©
ÎÒÏë¶à×ÐϸÑо¿Ð©¾ßÌåµÄÀý×Ó»áÓÐÖúÓÚ±à³Ìѧϰ°É£¬ÊÇô£¿

kkfly ·¢±íÓÚ 2006-4-20 20:18

ÓÉÓÚ¸Õѧ  ²»»áÓÃawk
ÎÒ×Ô¼ºÐ´µÄÒ»¸ö½Å±¾ ÖмäÓдíµÄµØ·½ ²»ÖªµÀÔõô¸Ä
ÎÒ×¢ÊͳöÀ´ÁË Çë½ÌÔõô¸Ä
лл¸÷λ´óÏÀÁË
#!/bin/sh
#¿ÎºóÁ·Ï°×îºóÒ»Ìâ,¼ÙÉèscore.txtÎļþÖеĿ¼ÊԳɼ¨¸ø³öµÄ¸ñʽΪ
# °à¼¶ ÐÕÃû µÃ·Ö
# 01 ÕÅÈý 50

echo "Please input the class id(01/02/03) that you want to qurey:"
read classid
num=0
totalscore=0
avarage=0
grep -c $classid score.txt>temp
read num<temp ="">
rm temp
echo "the students of class $classid is : $num"
index=2
score=null
while[ "$score" != '' ] ££ÅжÏscoreÊDz»ÊÇΪ¿ÕµÄ(½ØÈ¡µ½×îºó»á½ØÈ¡Ò»¸ö¿Õ´®£¿£¿) ÒòΪÎÒÓõÄÊÇcutÃüÁî
                                ££ÓдíÎó Ó¦¸ÃÔõôÅжÏÄØ £¿£¿
do
cut -f(3*index) socre.txt>temp ££ÎÒÏë×öµÃÊǽØÈ¡n*3¸öÓòÖеÄÊý×Ö
                                                ££(¸ù¾ÝÄÚÈݸñʽ)¿ÉÊDz»Öª¶Ô²»¶Ô
read score<temp ="">
rm temp
$index=£àexpr $index+1£à
totalscore=`expr $totalscore+$score`
done
avarage=`expr $totalscore/$num`
echo "the avarage score of the student is :$avarage"</temp></temp>

dearvoid ·¢±íÓÚ 2006-4-20 20:47

½¨ÒéÏÈ¿´¿´Öö¥µÄ¼¸¸öÌû×Ó

link-112233 ·¢±íÓÚ 2006-6-5 15:25

²»Ã÷°×ΪʲôҪÕâÑùд.ºÇºÇ!ÎÒ²»Ì«Àí½â.score=null
while[ "$score" != '' ] Õâ¸öµØ·½ÊÇÔõô»ØÊ°¡.

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.