请问这样的统计用shell如何做

sh/bash/dash/ksh/zsh等Shell脚本
回复
mushroom09
帖子: 16
注册时间: 2009-08-13 16:57

请问这样的统计用shell如何做

#1

帖子 mushroom09 » 2009-10-27 15:01

A :100
B:12
C:100
-----------------
A:50
B:12
C:23
统计结果要如下的:

A:总数
B:总数
C:总数

谢谢!!
头像
O_O_BOT
帖子: 2461
注册时间: 2009-05-20 19:32

Re: 请问这样的统计用shell如何做

#2

帖子 O_O_BOT » 2009-10-27 15:06

awk 数组
irc 聊天室
ubuntu-cn 的irc 频道为
irc.ubuntu.com 8001 #ubuntu-cn
UTF8编码 可用 irssi xchat pidgin weechat 登录

http://webchat.freenode.net/?channels=ubuntu-cn
[url]irc://irc.freenode.net/ubuntu-cn[/url]
mushroom09
帖子: 16
注册时间: 2009-08-13 16:57

Re: 请问这样的统计用shell如何做

#3

帖子 mushroom09 » 2009-10-27 15:16

能否详细些,初学shell ,多多指教 :em06
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 请问这样的统计用shell如何做

#4

帖子 aerofox » 2009-10-28 12:25

代码: 全选

awk -F: 'NF==2 {sum[$1]+=$2};  END {for (k in sum) print k ":" sum[k]}'
Snacl
帖子: 6
注册时间: 2009-04-08 20:50

Re: 请问这样的统计用shell如何做

#5

帖子 Snacl » 2009-10-28 13:50

:em04
头像
cnkilior
论坛版主
帖子: 4984
注册时间: 2007-08-05 17:40

Re: 请问这样的统计用shell如何做

#6

帖子 cnkilior » 2009-10-28 19:25

代码: 全选

#!/usr/bin/perl -w
while(<>){
     chopm;
      if (/A:(\d+)/){
            $a+=$1;
      }elsif(/B:(\d+)/){
            $b+=$1;
     }elsif(/C:(\d+)/){
            $c+=$1;
      }
}
回复