# thread count

**URL:** <https://community.unix.com/t/thread-count/242183>\
**Category:** Shell Programming and Scripting\
**Created:** [June 10, 2009, 7:38am UTC](https://community.unix.com/t/thread-count/242183 "2009-06-10T07:38:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![namishtiwari](https://community.unix.com/letter_avatar/namishtiwari/32/6_449c533f586939b6e5f246632a46421f.png) [@namishtiwari](https://community.unix.com/u/namishtiwari)\
**Post date:** [June 10, 2009, 7:38am UTC](https://community.unix.com/t/thread-count/242183/1 "2009-06-10T07:38:22Z")

</div>

Hi,

I have a log file in which there are plenty of threads. i want to count the number of unique threads.Here thread is tid value.  
My Logfile looks like this--

```nohighlight
Wed May 20 05:22:53.993 2009 Morocco Standard Time INFO: pid 2172 tid 688: 17: 10106931: ArAuthFrameworkImpl::doPreAuth::1:10106931:: Authentication mechanism returned [0] for AuthIdentity []
Wed May 20 05:22:53.993 2009 Morocco Standard Time INFO: pid 2172 tid 688: 170: 10106931: Arcot Native Server: recvd AA_BIN_MSG_VER_CHG
Wed May 20 05:22:57.634 2009 Morocco Standard Time INFO: pid 2172 tid 3352: 170: 10106932: Arcot Native Server: recvd AA_BIN_MSG_GET_WLT
Wed May 20 05:22:57.634 2009 Morocco Standard Time INFO: pid 2172 tid 3352: 170: 10106932: Session tracker Id associated with generate challenge[1:10106932]

```

Like this there are thousands of threads in a log file.  
i want to capture the unique threads and count the numbers.  
Any kind of help or suggestion will be very useful to me.

Thanks  
NT

---

<div class="post-metadata">

**Author:** ![rakeshawasthi](https://community.unix.com/user_avatar/community.unix.com/rakeshawasthi/32/224_2.png) [@rakeshawasthi](https://community.unix.com/u/rakeshawasthi)\
**Post date:** [June 10, 2009, 7:55am UTC](https://community.unix.com/t/thread-count/242183/2 "2009-06-10T07:55:00Z")

</div>

```nohighlight
 awk 'BEGIN { FS1="[:]+"}
{
        FS=FS1
        print $15
}' data > outfile

for tid in `cat outfile | /bin/sort -u`
do
        echo $tid appears `grep $tid outfile | wc -l` times.
done

```

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [June 10, 2009, 8:04am UTC](https://community.unix.com/t/thread-count/242183/3 "2009-06-10T08:04:10Z")

</div>

```nohighlight
perl -ne'
  / tid (\d+)/ && $tid{$1}++;
  print map "$_: $tid{$_}\n", keys %tid
    if eof
  ' infile

```

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [June 10, 2009, 8:06am UTC](https://community.unix.com/t/thread-count/242183/4 "2009-06-10T08:06:13Z")

</div>

Try this:

```nohighlight
awk '{a[$13]++}END{for(i in a){print i, a}}' file

```

---

<div class="post-metadata">

**Author:** ![namishtiwari](https://community.unix.com/letter_avatar/namishtiwari/32/6_449c533f586939b6e5f246632a46421f.png) [@namishtiwari](https://community.unix.com/u/namishtiwari)\
**Post date:** [June 10, 2009, 8:53am UTC](https://community.unix.com/t/thread-count/242183/5 "2009-06-10T08:53:25Z")

</div>

> [@radoulov](#):
>
> ```plaintext
> perl -ne'
> / tid (\d+)/ && $tid{$1}++;
> print map "$_: $tid{$_}\n", keys %tid
> if eof
> ' infile
> 
> ```

Hi ,

i am using this code and it's giving me whole count including the repated ones. I just want to get the count of unique threads. please suggest me what i need to do to achieve this.

```plaintext
#!/usr/bin/perl -w

print "Hello, World...\n";
my $logFile = $ARGV[0]; 
die "usage: $0 <logFile>" unless $logFile; 
die "Logfile $logFile doesn't exist" unless -f "$logFile"; 
open(my $log, "<", $logFile) or die "Can't open $logFile for reading."; 
print "Processing file $logFile...\n"; 
#my $authenticates = {}; 
my $n = 0;
my $TIDCount = 0;
while(my $line = <$log>) { 
    # Outer loop. Look for an interesting part of the log file. 
	$n++; 
    $line =~ tr/\r\n//d; 
	if($line =~ /tid (\d+)/){
		$TIDCount++;
        next;
	}
}
print "Thread count for the logfile is $TIDCount\n";

```

thanks  
NT

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [June 10, 2009, 8:58am UTC](https://community.unix.com/t/thread-count/242183/6 "2009-06-10T08:58:35Z")

</div>

You should use something like this:

```nohighlight
 ++$TIDcount 
    if / tid (\d+)/ && !$tidUnique{$1}++;

```

Given you sample data:

```nohighlight
% perl -nle'
  ++$c if / tid (\d+)/ && !$tid{$1}++;
  print $c if eof
  ' infile
2

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [June 10, 2009, 9:00am UTC](https://community.unix.com/t/thread-count/242183/7 "2009-06-10T09:00:49Z")

</div>

Borrowing from Franklin52:

```nohighlight
awk '{a[$13]++}END{for(i in a) if (a==1) uniq++; print uniq }' file

```

---

<div class="post-metadata">

**Author:** ![namishtiwari](https://community.unix.com/letter_avatar/namishtiwari/32/6_449c533f586939b6e5f246632a46421f.png) [@namishtiwari](https://community.unix.com/u/namishtiwari)\
**Post date:** [June 10, 2009, 9:16am UTC](https://community.unix.com/t/thread-count/242183/8 "2009-06-10T09:16:45Z")

</div>

> [@radoulov](#):
>
> You should use something like this:
> 
> ```plaintext
> ++$TIDcount 
> if / tid (\d+)/ && !$tidUnique{$1}++;
> 
> ```
> 
> Given you sample data:
> 
> ```plaintext
> % perl -nle'
> ++$c if / tid (\d+)/ && !$tid{$1}++;
> print $c if eof
> ' infile
> 2
> 
> ```

Hi Radoulov,

what is $tidUnique here? how it is going to work in my case. Can you suggest me in my code.  
when i used the code like this-

```plaintext
my $TIDCount = 0;
while(my $line = <$log>) { 
    # Outer loop. Look for an interesting part of the log file. 
	$n++; 
    $line =~ tr/\r\n//d; 
	++$TIDcount ;
    if /tid (\d+)/ && !$tidUnique{$1}++;
        next;
	
}
print "Thread count for the logfile is $TIDCount\n";

```

it gave me these errors  
syntax error at tid\_count.pl line 17, near "if /tid (\d+)/"  
syntax error at tid\_count.pl line 17, near "++;"  
syntax error at tid\_count.pl line 20, near "}"  
Execution of tid\_count.pl aborted due to compilation errors.

Thanks  
NT

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [June 10, 2009, 9:37am UTC](https://community.unix.com/t/thread-count/242183/9 "2009-06-10T09:37:09Z")

</div>

Try this:

```nohighlight
#!/usr/bin/perl

use warnings;
use strict;

print "Hello, World...\n";

my $logFile = shift;
my ( $TIDCount, %TIDUnique );

die "usage: $0 <logFile>\n" unless defined $logFile;

die "Logfile $logFile doesn't exist\n" unless -f $logFile;

open my $log, $logFile or die "Can't open $logFile for reading\n";

print "Processing file $logFile...\n";

while (<$log>) {
    ++$TIDCount
      if / tid (\d+)/ && !$TIDUnique{$1}++;
}

print "Thread count for the logfile is $TIDCount\n";

```

---

<div class="post-metadata">

**Author:** ![namishtiwari](https://community.unix.com/letter_avatar/namishtiwari/32/6_449c533f586939b6e5f246632a46421f.png) [@namishtiwari](https://community.unix.com/u/namishtiwari)\
**Post date:** [June 10, 2009, 10:07am UTC](https://community.unix.com/t/thread-count/242183/10 "2009-06-10T10:07:19Z")

</div>

Hi Radoulov,

Thank you very much, it worked for me.  
I need to dig alot to learn perl.

Thanks  
NT
