【问题标题】:Calculate numbers in file by bash script通过 bash 脚本计算文件中的数字
【发布时间】:2018-01-14 20:13:17
【问题描述】:

我有以下内容的文件。 例如2 个片段大小的图像。我想计算 bash 脚本中的片段总大小。

IMAGE admindb1 8 admindb1_1514997916 bus 4 Default-Application-Backup 2 3 1 1517676316 0 0
FRAG 1 1 10784 0 2 6 2 HSBRQ2 fuj 65536 329579 1514995208 60 0 *NULL* 1517676316 0 3 1 *NULL*

IMAGE admindb1 8 admindb1_1514995211 bus 4 Default-Application-Backup 2 3 1 1517673611 0 0
FRAG 1 1 13168256 0 2 6 12 HSBQ8I fuj 65536 173783 1514316708 52 0 *NULL* 1517673611 0 3 1 *NULL*
FRAG 1 2 24288384 0 2 6 1 HSBRJ7 fuj 65536 2 1514995211 65 0 *NULL* 0 0 3 1 *NULL*
FRAG 1 3 24288384 0 2 6 1 HSBRON fuj 65536 2 1514995211 71 0 *NULL* 0 0 3 1 *NULL*
FRAG 1 4 13806752 0 2 6 1 HSBRRK fuj 65536 2 1514995211 49 0 *NULL* 0 0 3 1 *NULL*

输出应该是这样的:

For Image admindb1_1514997916  total size is 10784
For Image  admindb1_1514995211  total size is 75551776

应计算以 FRAG 开头的行的第 4 列。

我的脚本不工作:

#!/bin/bash 
file1=/home/turgun/Desktop/IBteck/script-last/frags 
imagelist=/home/turgun/Desktop/IBteck/script-last/imagelist 
counter=1 
for counter in `cat $imagelist` 
do 
     n=`awk '/'$counter'/{ print NR; exit }' $file1`
     for n in `cat $file1` 
     do 
          if [[ ! $n = 'IMAGE' ]]; then 
              echo "For Image $counter total size is " \
                   `grep FRAG frags | awk '{print total+=$4}'`  
          fi 
     done 
done

【问题讨论】:

  • 到目前为止你尝试过什么?这与 javascript 有什么关系?
  • echo把结果转成JavaScript了吗?
  • 另外:哪一列是您想要求和的大小?我们应该猜测吗?
  • 通过标记 javascript 输入错误
  • @TurgunDuishonali,为什么要错过这个片段FRAG 1 1 13168256 ...?第二张图片的输出是For image admindb1_1514995211 total size is 75551776

标签: bash shell awk


【解决方案1】:
awk 'function summary() {print "For Image",image,"total size is",sum} 
     $1=="IMAGE" {image=$4}                         
     $1=="FRAG" {sum+=$4} 
     $1=="" {summary(); sum=0}
     END{summary()}' file

输出:

对于图像 admindb1_1514997916,总大小为 10784 对于图像 admindb1_1514995211,总大小为 75551776

我假设最后一行不为空。

【讨论】:

  • 嗨赛勒斯,感谢您的支持。只有一个问题。实际上在没有空格的文件中。我们可以写什么来代替它? ($1=="")。这样的行有上千条。
【解决方案2】:
cat -s file.txt | sed -n '/^IMAGE /{s:^[^ ]*  *[^ ]*  *[^ ]*  *\([^ ]*\).*$:echo -n "For image \1 total size is "; echo `echo ":;s:\*::g;p};/^$/{s:^:0"`|bc:;p};/^FRAG /{s:^[^ ]*  *[^ ]*  *[^ ]*  *\([^ ]*\).*$:\1\+:;s:\*::g;p};' | bash

输出:

For image admindb1_1514997916 total size is 10784
For image admindb1_1514995211 total size is 75551776

【讨论】:

    【解决方案3】:

    Awk解决方案:

    awk '/^IMAGE/{ 
             if (t) { printf "For image %s total size is %d\n",img,t; t=0 } img=$4 
         }
         /^FRAG/{ t+=$4 }
         END{ if (t) printf "For image %s total size is %d\n",img,t }' file
    

    输出:

    For image admindb1_1514997916 total size is 10784
    For image admindb1_1514995211 total size is 75551776
    

    【讨论】:

    • 你也是最好的解决方案
    【解决方案4】:

    cutGNU sed 的粗糙组合(不小心使用了e评估)和datamash

    cut -d' ' -f4 file | datamash --no-strict transpose | 
    sed 's#\tN/A\t#\n#g;y#\t# #' | 
    sed 's/ \(.*\)/ $((\1))/;y/ /+/;s/+/ /;
         s/\(.*\) \(.*\)/echo For image \1  total size is \2/e'
    

    输出:

    For image admindb1_1514997916 total size is 10784
    For image admindb1_1514995211 total size is 75551776
    

    Cyrus's answer 更好。这个答案显示了sed 的一些限制。此外,如果数据文件很大(例如数百万个数字相加),则用于将添加到 shell 的 e评估 可能会超过其命令行长度限制。

    【讨论】:

      猜你喜欢
      • 2011-06-28
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 2016-07-22
      • 1970-01-01
      • 2012-02-12
      相关资源
      最近更新 更多