how to read how many times same result occurs?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to read how many times same result occurs?
# 1  
Old 12-13-2011
how to read how many times same result occurs?

Is there a way to read how many times each result occurs in a list,

say the list.txt has the results:
a
c
d
a
f
c
a
d
e

and I need to know how many times each of them occurs such as :
a=3
c=2
..etc.
# 2  
Old 12-13-2011
Code:
awk '{a[$1]++}END{for (i in a) print i"="a[i]}' list.txt

# 3  
Old 12-13-2011
Code:
#!/usr/bin/bash

for result in a b c d e f
do
    echo "${result}=$(grep -c $result list.txt)"
done

Result:

Code:
a=3
b=0
c=2
d=2
e=1
f=1

# 4  
Old 12-13-2011
Code:
sort list.txt | uniq -c

# 5  
Old 12-13-2011
Thank you Smilie

learned again something new..
# 6  
Old 12-13-2011
Another way:
Code:
sort list.txt |uniq -c

Edit: CarloM was a few seconds faster...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace one value which occurs multiple times

Hi, My Input File : "MN.1.2.1.2.14.1.1" := "MN_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) "MOS.1.2.1.2.13.6.2" := "MOS_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) Like above template,I have... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

2. Shell Programming and Scripting

Counting number of times content in columns occurs in other files

I need to figure out how many times a location (columns 1 and 2) is present within a group of files. I figured using a combination of 'while read' and 'grep' I could count the number of instances but its not working for me. cat file.txt | while read line do grep $line *08-new.txt | wc -l... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

3. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Windows & DOS: Issues & Discussions

Read command result as variable

Hi all, Is there a simple way to read a command output as a variable? I've running a batch file to do: attrib.exe * | find /c /v "" >filecount.txt but rather than write it to the txt file I'd like to read the total in as a variable to pass to the rest of the bat file... Any help much... (1 Reply)
Discussion started by: Grueben
1 Replies

5. Shell Programming and Scripting

Removal of a tag in the xml which occurs mutiple times

Hi, The tag can occur multiple times. I want to remove entire <SeqNum>..</SeqNum> from each line, regardless the values with in this tag. for each line value inside these tags could be different. So please suggest how to do this. Note: Each profile information is in one line. ... (2 Replies)
Discussion started by: Anusha_Reddy
2 Replies

6. Shell Programming and Scripting

how to read a certain column from the status result

I am using this script to get the status of the running process VAR=$(ps -ef | grep batch_proc_x | grep -v grep) echo $VAR now it returns this splgwin 13195 13100 0 14:13:13 pts/tb 0:00 /usr/bin/ksh ./batch_proc_x.sh now is there a way to read that time 14:13:13 directly from... (2 Replies)
Discussion started by: akabir77
2 Replies

7. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

8. Shell Programming and Scripting

TO find the word which occurs maximum number of times

Hi Folks !!!!!!!!!!!!!!!!!!! My Requirement is............. i have a input file: 501,501.chan 502,502.anand 503,503.biji 504,504.raja 505,505.chan 506,506.anand 507,507.chan and my o/p should be chan->3 i.e. the word which occurs maximum number of times in a file should be... (5 Replies)
Discussion started by: aajan
5 Replies

9. Shell Programming and Scripting

Trying to read data multiple times

I am developing a script to automate Global Mirroring on IBM DS8100's. Part of the process is to establish a global copy and wait until the paired LUN's Out of Sync tracks goes to zero. I can issue a command to display the ouput and am trying to use AWK to read the appropriate field. I am... (1 Reply)
Discussion started by: coachr
1 Replies

10. Shell Programming and Scripting

Trying to read data multiple times

I am developing a script to automate Global Mirroring on IBM DS8100's. Part of the process is to establish a global copy and wait until the paired LUN's Out of Sync tracks goes to zero. I can issue a command to display the ouput and am trying to use AWK to read the appropriate field. I am... (0 Replies)
Discussion started by: coachr
0 Replies
Login or Register to Ask a Question