2

我正在试验 OpenMP,我只是想编写一个小的 shell 脚本来计算每个线程输出的行数。并且简单地吐出每个计数。我开始进入它,并开始编写一个带有硬编码或参数化上限的 for 循环,以及 grep -c REG_EX,我还尝试使用 sed 首先清理额外的输出以使 greps 工作更容易,但它不像我想要的那样工作

EXAMPLE PROGRAM OUT(SCRIPT INPUT)

Output to STDOUT (I can obviously pipe into sort first):
Thread 0: EXTRA OUTPUT
Thread 0: EXTRA OUTPUT
Thread 2: EXTRA OUTPUT
Thread 3: EXTRA OUTPUT
Thread 0: EXTRA OUTPUT
Thread 1: EXTRA OUTPUT
.
.
.

ETC

我需要的是:

Thread 0: #repeats
Thread 1: #repeats
.
.
.
Thread n: #repeats

提前致谢

4

6 回答 6

4

Just pipe your output into this:

grep -o "Thread [0-9]*" | sort | uniq -c | awk '{print $2, $3 ":", $1}'

This will first reduce each line to just the part before the colon (so that every line a given thread outputs is identical), count how many lines each thread output, and rearrange the output of uniq to match your sample output.

于 2013-05-14T00:14:20.600 回答
1

You can pipe the output of grep to wc ("Word count") which, with a -l flag, will count the number of lines:

grep needle haystack.txt | wc -l
于 2013-05-14T00:08:23.220 回答
1
for i in {0..10}; do
   str="Thread $i:"
   cnt=$(grep -c "$str" input)
   echo "$str $cnt"
done
于 2013-05-14T00:09:17.890 回答
1

I think awk alone is enough. This should work for any number of threads..

awk -F ":| " '{a[$2]++; if($2>max) max=$2;} END {for (i=0; i<=max; i++) print "Thread "i": "a[i]}' output

For your example, it will produce..

Thread 0: 3
Thread 1: 1
Thread 2: 1
Thread 3: 1
于 2013-05-14T00:18:07.577 回答
0

要获取与模式匹配的行,请使用

grep -v 'pattern' file

要获得可以做到的线条,

grep 'pattern' file

要数,

grep -v 'foo' bar.txt | wc -l (与 'foo' 不匹配的行数)

grep 'foo' bar.txt | wc -l (匹配 'foo' 的行数)

This seems to be what the title asks for, but I have to admit, your post confused me

于 2013-05-14T00:06:25.890 回答
0

All you need is awk:

<infile awk '{ h[$1" "$2]++ } END { for(k in h) print k, h[k] }'

Output:

Thread 0: 3
Thread 1: 1
Thread 2: 1
Thread 3: 1

If you only want to count lines beginning with "Thread" prepend the first block like this: $1 == "Thread" { h[$1" "$2]++ } ....

于 2013-05-14T07:22:42.547 回答