读书人

linux脚本

发布时间: 2012-09-02 21:00:34 作者: rapoo

linux脚本求助?
需要处理一个文件数据, 文件中的格式如下,想要把第一列的数据加起来,求和。
文件名称a.txt:
数据内容如下:
<text>4 bytes in 1 blocks are possibly lost in loss record 9 of 374</text>
<text>6 bytes in 1 blocks are possibly lost in loss record 12 of 374</text>
<text>8 bytes in 1 blocks are possibly lost in loss record 15 of 374</text>
<text>10 bytes in 1 blocks are possibly lost in loss record 19 of 374</text>
<text>12 bytes in 1 blocks are possibly lost in loss record 23 of 374</text>
<text>16 bytes in 1 blocks are possibly lost in loss record 43 of 374</text>
.........
.........
<text>16 bytes in 1 blocks are possibly lost in loss record 44 of 374</text>
<text>16 bytes in 1 blocks are possibly lost in loss record 45 of 374</text>
<text>16 bytes in 1 blocks are possibly lost in loss record 46 of 374</text>
<text>16 bytes in 1 blocks are possibly lost in loss record 47 of 374</text>
<text>16 bytes in 1 blocks are possibly lost in loss record 48 of 374</text>
<text>16 bytes in 1 blocks are possibly lost in loss record 49 of 374</text>
<text>16 bytes in 1 blocks are possibly lost in loss record 50 of 374</text>

最好用awk,其他的脚本也行。
谢谢
ps:
awk '{print $1}' a.txt
输出:
<text>4
<text>6
<text>8
<text>10
<text>12
<text>16
<text>16
<text>16
<text>16
<text>16
<text>16
<text>16
<text>16
<text>16
<text>16
<text>16
<text>24
<text>26

[解决办法]
怎么在这里问这个问题
awk 'BEGIN{count=0; while((getline<"a.txt") > 0) { {gsub(/<text>/,"") $1}; count=count+$1;} print count}'

读书人网 >C++

热点推荐