Sponsored Content
Top Forums Shell Programming and Scripting awk to count using each unique value Post 302900365 by ncwxpanther on Tuesday 6th of May 2014 10:36:25 AM
Old 05-06-2014
awk to count using each unique value

Im looking for an awk script that will take the unique values in column 5, then print and count the unique values in column 6.

Code:
CA001011500  11111 11111 -9999 201301 AAA
CA001012040  11111 11111 -9999 201301 AAA
CA001012573  11111 11111 -9999 201301 BBB
CA001012710  11111 11111 -9999 201301 BBB
CA001015105  11111 11111 -9999 201301 BBB
CA001015638  11111 11111 -9999 201301 CCC
CA001011500  11111 11111  -9999 201302 AAA
CA001012040  11111 11111  -9999 201302 BBB
CA001012573  11111 11111 -9999 201302 BBB
CA001012710  11111  11111 -9999 201302 CCC
CA001015105  11111 11111  -9999 201302 CCC
CA001015638  11111 11111  -9999 201302 CCC

Output will need to be:

Code:
201301 AAA 2
201301 BBB 3
201301 CCC 1
201302 AAA 1
201302 BBB 2
201302 CCC 3

I know this will print the unique values of column 5, but how do i tie in the count of unique values in column 6?
Code:
 awk '{ a[$5]++ } END { for (b in a) { print b } }'

Thanks in advance!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to count unique strings

How do I count the total number of unique strings from a file using Perl? Any help is appreciated.. (6 Replies)
Discussion started by: my_Perl
6 Replies

2. Shell Programming and Scripting

awk count how many unique IPs have received that error

Hi all, I want to write a awk script that counts unique IPs that have received one special error. For example 25-04-2012;192.168.70.31;1254545454545417;500.0;SUCCESS 25-04-2012;192.168.70.32;355666650914;315126423993;;General_ERROR_23 30-04-2012;192.168.70.33;e;null;null;Failure... (2 Replies)
Discussion started by: arrals_vl
2 Replies

3. Shell Programming and Scripting

awk pattern match and count unique in column

Hi all I have a need of searching some pattern in file by month and then count unique records D11 G11 R11 -------> Pattern available in file S11 Jan$1 to $5 column contains some records in which I want to find unique for this purpose I have written script like below awk '/Jan/ ||... (4 Replies)
Discussion started by: nex_asp
4 Replies

4. UNIX for Dummies Questions & Answers

Search and count a unique string

Hi Guys, I have a file as follows. Here is my story: For each field, the string in the 5th column needs to be searched in other fields of the same column and counted if the 1st column of the field is different from that of the primary field. BTW, the unique strings of 1st column need to be... (6 Replies)
Discussion started by: a_bahreini
6 Replies

5. Shell Programming and Scripting

awk unique count of partial match with semi-colon

Trying to get the unique count of the below input, but if the text in beginning of $5 is a partial match to another line in the file then it is not unique. awk awk '!seen++ {n++} END {print n}' input 7 input chr1 159174749 159174770 chr1:159174749-159174770 ACKR1 chr1 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

6. Shell Programming and Scripting

Count occurrence of column one unique value having unique second column value

Hello Team, I need your help on the following: My input file a.txt is as below: 3330690|373846|108471 3330690|373846|108471 0640829|459725|100001 0640829|459725|100001 3330690|373847|108471 Here row 1 and row 2 of column 1 are identical but corresponding column 2 value are... (4 Replies)
Discussion started by: angshuman
4 Replies

7. UNIX for Beginners Questions & Answers

Awk: count unique element of array

Hi, tab-separated input: blabla_1 A,B,C,C blabla_2 A,E,G blabla_3 R,Q,A,B,C,R,Q output: blabla_1 3 blabla_2 3 blabla_3 5 After splitting $2 in an array, I am trying to store the number of unique elements in a variable, but have some difficulties resetting the variable to 0 before... (6 Replies)
Discussion started by: beca123456
6 Replies

8. UNIX for Beginners Questions & Answers

Count unique words

Dear all, I would like to know how to list and count unique words in thousands number of text files. Please help me out thanks in advance (9 Replies)
Discussion started by: imranrasheedamu
9 Replies

9. UNIX for Beginners Questions & Answers

Count unique column

Hello, I am trying to count unique rows in my file based on 4 columns (2-5) and to output its frequency in a sixth column. My file is tab delimited My input file looks like this: Colum1 Colum2 Colum3 Colum4 Coulmn5 1.1 100 100 a b 1.1 100 100 a c 1.2 200 205 a d 1.3 300 301 a y 1.3 300... (6 Replies)
Discussion started by: nans
6 Replies

10. UNIX for Beginners Questions & Answers

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies
IO::Async::Function(3pm)				User Contributed Perl Documentation				  IO::Async::Function(3pm)

NAME
"IO::Async::Function" - call a function asynchronously SYNOPSIS
use IO::Async::Function; use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $function = IO::Async::Function->new( code => sub { my ( $number ) = @_; return is_prime( $number ); }, ); $loop->add( $function ); $function->call( args => [ 123454321 ], on_return => sub { my $isprime = shift; print "123454321 " . ( $isprime ? "is" : "is not" ) . " a prime number "; }, on_error => sub { print STDERR "Cannot determine if it's prime - $_[0] "; }, ); $loop->run; DESCRIPTION
This subclass of IO::Async::Notifier wraps a function body in a collection of worker processes, to allow it to execute independently of the main process. The object acts as a proxy to the function, allowing invocations to be made by passing in arguments, and invoking a continuation in the main process when the function returns. The object represents the function code itself, rather than one specific invocation of it. It can be called multiple times, by the "call" method. Multiple outstanding invocations can be called; they will be dispatched in the order they were queued. If only one worker process is used then results will be returned in the order they were called. If multiple are used, then each request will be sent in the order called, but timing differences between each worker may mean results are returned in a different order. Since the code block will be called multiple times within the same child process, it must take care not to modify any of its state that might affect subsequent calls. Since it executes in a child process, it cannot make any modifications to the state of the parent program. Therefore, all the data required to perform its task must be represented in the call arguments, and all of the result must be represented in the return values. The Function object is implemented using an IO::Async::Routine with two IO::Async::Channel objects to pass calls into and results out from it. The "IO::Async" framework generally provides mechanisms for multiplexing IO tasks between different handles, so there aren't many occasions when such an asynchronous function is necessary. Two cases where this does become useful are: 1. When a large amount of computationally-intensive work needs to be performed (for example, the "is_prime" test in the example in the "SYNOPSIS"). 2. When a blocking OS syscall or library-level function needs to be called, and no nonblocking or asynchronous version is supplied. This is used by "IO::Async::Resolver". This object is ideal for representing "pure" functions; that is, blocks of code which have no stateful effect on the process, and whose result depends only on the arguments passed in. For a more general co-routine ability, see also IO::Async::Routine. PARAMETERS
The following named parameters may be passed to "new" or "configure": code => CODE The body of the function to execute. min_workers => INT max_workers => INT The lower and upper bounds of worker processes to try to keep running. The actual number running at any time will be kept somewhere between these bounds according to load. max_worker_calls => INT Optional. If provided, stop a worker process after it has processed this number of calls. (New workers may be started to replace stopped ones, within the bounds given above). idle_timeout => NUM Optional. If provided, idle worker processes will be shut down after this amount of time, if there are more than "min_workers" of them. exit_on_die => BOOL Optional boolean, controls what happens after the "code" throws an exception. If missing or false, the worker will continue running to process more requests. If true, the worker will be shut down. A new worker might be constructed by the "call" method to replace it, if necessary. setup => ARRAY Optional array reference. Specifies the "setup" key to pass to the underlying IO::Async::Process when setting up new worker processes. METHODS
$function->start Start the worker processes $function->stop Stop the worker processes $function->restart Gracefully stop and restart all the worker processes. $function->call( %params ) Schedules an invocation of the contained function to be executed on one of the worker processes. If a non-busy worker is available now, it will be called immediately. If not, it will be queued and sent to the next free worker that becomes available. The request will already have been serialised by the marshaller, so it will be safe to modify any referenced data structures in the arguments after this call returns. The %params hash takes the following keys: args => ARRAY A reference to the array of arguments to pass to the code. on_result => CODE A continuation that is invoked when the code has been executed. If the code returned normally, it is called as: $on_result->( 'return', @values ) If the code threw an exception, or some other error occured such as a closed connection or the process died, it is called as: $on_result->( 'error', $exception_name ) on_return => CODE and on_error => CODE An alternative to "on_result". Two continuations to use in either of the circumstances given above. They will be called directly, without the leading 'return' or 'error' value. $count = $function->workers Returns the total number of worker processes available $count = $function->workers_busy Returns the number of worker processes that are currently busy $count = $function->workers_idle Returns the number of worker processes that are currently idle NOTES
For the record, 123454321 is 11111 * 11111, a square number, and therefore not prime. AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-10-24 IO::Async::Function(3pm)
All times are GMT -4. The time now is 05:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy