简体   繁体   English

使用awk和grep进行累加

[英]Using awk and grep to add up

If I had a file that looks like this: 如果我有一个看起来像这样的文件:

23.00   33.44 abcd 44.44  abcd12345abcd
33.00   22.22 qt   44.00  zlkm12345ksda

...and I wanted to add up the first column whenever I encounter 12345 in the middle of the pattern in the fifth column, how would I go about doing that? ...并且我想在第五列的模式中间遇到12345时将第一列加起来,我该怎么做呢?

Something like this? 像这样吗

awk '$5 ~ /12345/ { TOT = TOT + $1 } END { print TOT + 0 }' yourFile.txt

(Not at a computer, so my syntax might be a bit off.) (不在计算机上,因此我的语法可能有点错误。)

The first bit selects the lines you want and updates the total while the END bit just prints what's accumulated. 第一位选择所需的行并更新总数,而END位仅打印累积的行。

No grep needed (for almost all intents and purposes, awk is just as good if not better), and the search is limited to just the column you want searched. 不需要grep(对于几乎所有意图和目的,awk都是一样好,甚至更好),并且搜索仅限于要搜索的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM