Counting the number of repetition of a pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Counting the number of repetition of a pattern
# 1  
Old 03-24-2011
Question Counting the number of repetition of a pattern

the o/p of a command is
Code:
-bash-2.05b# grep -o a *
cc:a
dd:a
a
dd:a
office:a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a


how to count how many "a" are there in each file,, cc, dd & office are the file names
# 2  
Old 03-24-2011
Do you need the number of line containing one or more "a" ?

Or do you need the number of "a" ?
Code:
for i in *
do
echo "$i :"
sed 's/[^a]//g' $i | fold -w 1 | uniq -c
done

Or do you need the number of "a" ?
Code:
for i in cc dd office
do
echo "$i :"
sed 's/[^a]//g' $i | fold -w 1 | uniq -c
done

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 03-24-2011
but listen, this is not my Q,, I was assign a task.. where I have to find all those filenames, inside the files where the pattern "a" is used only once..
Quote:
I wrote a script..
Code:
for i in *; do if [ `grep -o "a" $i | wc -l` -eq 1 ]; then echo "file is "$i; fi; done

this works fine... but I was asked in interview, how to perform it by command
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help: Counting values less than a number

So I have several files (35000, to be exact) in the format rmsd_protein_*.dat each with 2 columns and 35000 rows. I would like to count how many values in the second column are less than 3 for each file, and output it into a new file so that it ultimately appears as: 1 14057 2 ... (12 Replies)
Discussion started by: Alexandryne
12 Replies

2. Shell Programming and Scripting

Counting Pattern and unique pattern

Hi, I have a log file which amongst other text has these lines: (id is always the same format - ) e.g. <username>user1</username> <name>fdfsdf</name> Multiple other tags <id>A111</id> <username>user2</username> <name>fdfsdf</name> Multiple other tags <id>A222</id>... (1 Reply)
Discussion started by: arsenalfan01
1 Replies

3. Shell Programming and Scripting

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

4. Shell Programming and Scripting

counting number of pattern occurrences

Hi All, Is it possible to count number of occurrences of a pattern in a single record using awk?? for example: a line like this: abrsjdfhafa I want to count the number of a character occurrences. but still use the default RS, I don't want to set RS to single character. (1 Reply)
Discussion started by: ghoda2_10
1 Replies

5. Shell Programming and Scripting

counting number of sentence

Hi all I want to count total numbers of sentences separated by fullstop (.) in different files under a directory at one go. Any help is appreciated. (3 Replies)
Discussion started by: my_Perl
3 Replies

6. Shell Programming and Scripting

counting the number of occurences

say i've got a text file with >10million sequences: ssss ssss tttttt uuuuuu uuuuuu uuuuuu ... I'd like to convert the file so that the output will report the number of occurence right by each sequence: 2 ssss 2 ssss 1 tttttt 3 uuuuuu 3 uuuuuu 3 uuuuuu .... (3 Replies)
Discussion started by: johjoh
3 Replies

7. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

8. Shell Programming and Scripting

counting the number of lines - again

Hi all, I use bash shell and I have a problem with wc. I would like to determine the number of lines in a file so I do wc -l filename but I don't want to get the filename again I just would like to have the number of lines and use it in a variable. Can anybody help? Thank you, (7 Replies)
Discussion started by: f_o_555
7 Replies

9. UNIX for Dummies Questions & Answers

Counting number of occurences

Hi All, I have to count the number of occurences of the character " ; " in a given line. I had used the following awk command to achieve the same echo $KOP.dat|awk '{split($1,my,";"); for(i in my)c++ }END{print c-1}' My file KOP.dat had the following data ... (1 Reply)
Discussion started by: kingofprussia
1 Replies

10. Linux

counting the number of lines

Hello, I have afile which begins with a few urls on multiple lines and then there is listing of some information on separate lines. The listing begins with the word Name on a given line followed by teh actual list. I want to count the number of lines in this file after the line having... (6 Replies)
Discussion started by: nayeemmz
6 Replies
Login or Register to Ask a Question