Grep command to count characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep command to count characters
# 1  
Old 01-19-2010
Grep command to count characters

I have a file that I need to count the number of "Y"s in the file, but just the "Y"s that fall between certain columns. Like:

file1:
Code:
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBB

I want to count the 'Y's between 17 and 21 = 2 "Y"s

Thanks

Last edited by Franklin52; 01-19-2010 at 08:41 AM.. Reason: Please use code tags!
# 2  
Old 01-19-2010
Try this:

Code:
awk '{
  s=substr($0,17,5)
  n+=gsub("Y","",s)
}
END{print n}
' file

# 3  
Old 01-19-2010
Code:
$ cat input-file
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBB
$ sed 's/^.\{17\}\(.\)\(.\)\(.\)\(.\).*$/\1\n\2\n\3\n\4/' input-file  | grep -c 'Y'
2

# 4  
Old 01-19-2010
thegeek,

grep -c gives the count of the lines that contain the pattern, not the numbers of the pattern in a line.

Regards
# 5  
Old 01-19-2010
(thegeek is outputting characters 17-21 with a newline after each character then counting the number of lines containing a letter "Y" ). Ingenious.
# 6  
Old 01-19-2010
Tools Here is another approach

Code:
>cat they.txt
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBBB
BBBBBBYBBBBYBBYBBBBBYBBBB
BBBBBBYBBBBYBBYBYBBBBBBBB

>cut -c17-21 they.txt | sed "s/Y/Y\n/" | grep "Y" | wc -l
4
>cut -c17-21 they.txt | sed "s/Y/Y\n/" | grep "Y" -c
4


Last edited by joeyg; 01-19-2010 at 11:16 AM.. Reason: added a 2nd command
# 7  
Old 01-19-2010
Just for the fun of it, another similar approach but using "fold" to break each character in each line into a separate line.

Code:
cut -c17-21 filename|fold -w 1| grep "Y"|wc -l

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count consecutive characters

Need to count consecutive characters in a string and give the output as below i/p=aaaabbcaa o/p=a4b2c1a2 (10 Replies)
Discussion started by: prasanna2166
10 Replies

2. Shell Programming and Scripting

How to count number of characters of wc -l output?

I want count number of characters / find the length of the 'wc -l' output This is the command bash-3.2$ gzcat /home/sid/file1.dat |wc -l 830752 So final out I want is 6 i.e lenght of 830752 I tried with awk bash-3.2$ gzcat /home/sid/file1.dat |wc -l | awk '{print length ($0)... (3 Replies)
Discussion started by: sidnow
3 Replies

3. Shell Programming and Scripting

Count characters after a line

Hi All! I would like to solve a problem but I have no clue of how do it!I will be grateful if someone could help me! I have a file like this: > genes | transcript ...sequence.... >ENSMUSG00000006638|ENSMUST00000006814 GGGAAATGGAATACCCCTACACAACCAAGATGCTGAGTTCCTCCCTGAGCCCGCAAAACG... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

4. UNIX for Dummies Questions & Answers

count different characters from one column

Hi everyone, and thanks after all I'm a biologist and i have to extract information from one text. The text is something like this 1023 A A 56 0 cc...,,,,..gg..Cc.c,,c..CC..,, 1024 T T 86 0 ..,,,..aaAA..,,aAA,,a,,A,,a 1025 G G 125 0 ... (5 Replies)
Discussion started by: beajorrin
5 Replies

5. UNIX for Dummies Questions & Answers

deletion of duplicate characters and count

to delete the duplicate characters in a file I used this code cat file.txt|tr -s "" tell the other ways using sed command to count of duplicate characters thanks:) (0 Replies)
Discussion started by: tsurendra
0 Replies

6. UNIX for Dummies Questions & Answers

Count the characters in a string

Hi all, I like to know how to get the count of each character in a given word. Using the commands i can easily get the output. How do it without using the commands ( in shell programming or any programming) if you give outline of the program ( pseudo code ) i used the following commands ... (3 Replies)
Discussion started by: itkamaraj
3 Replies

7. UNIX for Dummies Questions & Answers

Unix command to count the number of files with specific characters in name

Hey all, I'm looking for a command that will search a directory (and all subdirectories) and give me a file count for the number of files that contain specific characters within its filename. e.g. I want to find the number of files that contain "-a.jpg" in their name. All the searching I've... (6 Replies)
Discussion started by: murphysm
6 Replies

8. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

9. Shell Programming and Scripting

Count occurance of multiple strings using grep command

How to grep multiple string occurance in input file using single grep command? I have below input file with many IDP, RRBE messages. Out put should have count of each messages. I have used below command but it is not working grep -cH "(sent IDP Request)(Recv RRBCSM)" *.txt ... (5 Replies)
Discussion started by: sushmab82
5 Replies

10. UNIX for Dummies Questions & Answers

Sorting using count, grep and count

Hi, I trying to sort information in a file by making count for every object. For example: A B A D A B C i would like to sort out count for each object (A's, B's and etc) but in actual case i dont know the object but i know the position ofthe object. do i need to use array as well? Any... (2 Replies)
Discussion started by: sukhpal_78
2 Replies
Login or Register to Ask a Question