Shell脚本用于计算位于”nmemb:(“和”)”之间的数字。

huangapple go评论48阅读模式
英文:

Shell script to sum number between "nmemb:(" and ")"

问题

cat X | grep -o 'nmemb[^_]*$' | doSumBetweenBracket()

英文:

I have a file called "X" which has a lot of nmemb:(123) , nmemb:(56789) , ....

cat X | grep -o 'nmemb[^_]*$'

output like :

nmemb:(16384)
nmemb:(16384)
nmemb:(16384)
nmemb:(5608)
nmemb:(10776)
nmemb:(16384)
nmemb:(16384)
nmemb:(16384)
nmemb:(16384)
nmemb:(16384)

How can I sum these number between "nmemb:(" & ")" ? like :

cat X | grep -o 'nmemb[^_]*$' | doSumBetweenBracket()

答案1

得分: 2

#### 像这样:

    $ grep -oP 'nmemb:\(\K\d+' X | paste -sd '+' | bc


    grep -oP 'nmemb:\(\K\d+' X | awk '{c+=$1}END{print c}'

或者仅使用`GNU` `awk`(没有管道)

    awk '/memb:\([0-9]/{match($0, /[0-9]+/, a);c+=a[0]}END{print c}' X

#### 输出

    147456
英文:

Like this:

$ grep -oP 'nmemb:\(\K\d+' X | paste -sd '+' | bc

or

grep -oP 'nmemb:\(\K\d+' X | awk '{c+=$1}END{print c}'

or with GNU awk only (no pipes)

awk '/memb:\([0-9]/{match($0, /[0-9]+/, a);c+=a[0]}END{print c}' X

Output

147456

huangapple
  • 本文由 发表于 2023年2月19日 20:36:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定