finding out how many times something comes up


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting finding out how many times something comes up
# 1  
Old 02-01-2010
finding out how many times something comes up

Hi,

I have a file that looks something like this:

Code:
YPA124  NV
YPA124  NV
YPA124  V
YAR198  V
YAR198  V
YOB911  V
YOB911  V
YOB911  NV
YOB911  NA

So what i want to do is find out how many times each name on column 1 is represented by indicators on column 2.

So the output would look like this (column 1 would be the name, 2 would be the indicator and 3 would be how many times that indicator comes up per name):

Code:
YPA124  NV  2
YPA124  V    1
YAR198  V    2
YOB911  V    2
YOB911  NV  1
YOB911  NA   1

obviously I do not know how to do this so any help would be gladly appreciated.

thanks
# 2  
Old 02-01-2010
If the file is not sorted, then sort explicitly
Code:
uniq -dc file

# 3  
Old 02-01-2010
Hi,

uniq -c writes the number of repetations at the beginning of the line , with a trick u can change it:

Code:
uniq -c FILE.txt | awk '{printf "%s %-4s %-s\n",$2,$3,$1}'

output:

Code:
YPA124 NV   2   
YPA124 V    1   
YAR198 V    2   
YOB911 V    2   
YOB911 NV   1   
YOB911 NA   1

# 4  
Old 02-01-2010
Code:
awk ' { arr[$0]++} END{for (i in arr){print arr[i], i }} ' file > newfile

# 5  
Old 02-02-2010
Code:
while(<DATA>){
	chomp;
	my @tmp = split;
	$hash{$tmp[0]}->{$tmp[1]}++;
	$hash{$tmp[0]}->{_SEQ_}=$.
}
foreach my $key(sort {$hash{$a}->{_SEQ_} <=> $hash{$b}->{_SEQ_}} keys %hash){
	my %tmp = %{$hash{$key}};
	foreach my $k( grep {$_ ne "_SEQ_"}  keys %tmp){
		print $key," ",$k," ",$tmp{$k},"\n";
	}
}
__DATA__
YPA124  NV
YPA124  NV
YPA124  V
YAR198  V
YAR198  V
YOB911  V
YOB911  V
YOB911  NV
YOB911  NA

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Welcome in these 1337 times

Have you noticed that the Unix time right now starts with the combination "1337"? 1337 times indeed. ;-)) bakunin (10 Replies)
Discussion started by: bakunin
10 Replies

2. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

3. Shell Programming and Scripting

Listing times from ls

Hello, Im new to shell scripting and i am trying to write a part of my script that will search for all files in any given folder and write down all the names of the files and the atime, change time, and modtime of the files in one file as an output. I know that ls -l, ls -ul and ls -lc will give... (1 Reply)
Discussion started by: jnagda
1 Replies

4. HP-UX

grep for x for m times ',\{11\}'

All: OS version HP-UX ga016a501 B.11.31 U ia64 from the command prompt -grep for 1 to 11 occurences of "," returns both rows from the command prompt -grep for 11 occurences of "," returns 0 rows - should be 1 row. Any ideas - why? ga016a501 -> grep ',\{1,11\}' test3 | more ... (7 Replies)
Discussion started by: Bill L.
7 Replies

5. Shell Programming and Scripting

Executes many times

The code prints many times, "1st loop" and "2nd loop". How can I come out the if the condition is matched. Match should be only once and it should come out of the loop if the match is found time=$(date +"%H:%M:%S") echo $time while : do if then echo "1st loop" fi if then... (5 Replies)
Discussion started by: sandy1028
5 Replies

6. Shell Programming and Scripting

Getting logs between two times

Hi, I need to read log file for the period between two time frame.eg all those logs between 3AM to 4M. Any command to use for the shell scripting? Ahamed (5 Replies)
Discussion started by: ahamed
5 Replies

7. Shell Programming and Scripting

Exit if between 2 times

Hello, I am working in Solaris 10 with a Bash script and trying to figure out how I can tell the script to look at the day of the week and the time and for example if it is Sat between 5:50am and 6:30am then just exit out of the script. Any other day or time keep running. We have a... (3 Replies)
Discussion started by: LRoberts
3 Replies

8. AIX

how would you know your server was rebooted 3 times or 5 times

Is there such location or command to know how many times did you reboot your server in that particular day?in AIX. (3 Replies)
Discussion started by: kenshinhimura
3 Replies

9. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

10. UNIX for Advanced & Expert Users

Boot times

Hey all, What we have at work is a Themis board (VME rack SPARC system). We have to try to have the boot time as fast as can be. What we have already done is make some filesystems read only so if power is lost then the filesystem check will not run. This is only done to the partitions that... (2 Replies)
Discussion started by: woofie
2 Replies
Login or Register to Ask a Question