Grep command to count characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep command to count characters
# 8  
Old 01-19-2010
Quote:
Originally Posted by methyl
(thegeek is outputting characters 17-21 with a newline after each character then counting the number of lines containing a letter "Y" ). Ingenious.
But it doesn't give the desired output:

Code:
$ cat file
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBBB
BBBBBBYBBBBYBBYBBBBBYBBBB
BBBBBBYBBBBYBBYBYBBBBBBBB
$ sed 's/^.\{17\}\(.\)\(.\)\(.\)\(.\).*$/\1\n\2\n\3\n\4/' file|grep -c 'Y'
3

# 9  
Old 01-19-2010
Quote:
Originally Posted by Franklin52
But it doesn't give the desired output:

Code:
$ cat file
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBBB
BBBBBBYBBBBYBBYBBBBBYBBBB
BBBBBBYBBBBYBBYBYBBBBBBBB
$ sed 's/^.\{17\}\(.\)\(.\)\(.\)\(.\).*$/\1\n\2\n\3\n\4/' file|grep -c 'Y'
3

I wouldn't describe it as "ingenious" by any means, but it might work - on Linux at least - even if it does only takes characters 18 to 21 Smilie
# 10  
Old 01-19-2010
It is not because of grep. It is because of the characters which i have selected.

It should work now,
Code:
sed 's/^.\{16\}\(.\)\(.\)\(.\)\(.\)\(.\).*$/\1\n\2\n\3\n\4\n\5/' file | grep -c 'Y'


Selected from the character 17 to 21 as expected.
# 11  
Old 01-19-2010
Quote:
Originally Posted by thegeek
It is not because of grep. It is because of the characters which i have selected.

It should work now,
Code:
sed 's/^.\{16\}\(.\)\(.\)\(.\)\(.\)\(.\).*$/\1\n\2\n\3\n\4\n\5/' file | grep -c 'Y'


Selected from the character 17 to 21 as expected.
Right! Also with fold:

Code:
sed 's/^.\{16\}\(.\{5\}\).*$/\1/' a |fold -w 1 |grep -c 'Y'

methyl solution without the wc command is shorter:

Code:
cut -c17-21 filename|fold -w 1| grep -c 'Y'

# 12  
Old 01-19-2010
Thanks everyone for your suggestions... all of them worked
# 13  
Old 01-19-2010
People, people, people. Can we please stop the process forking insanity? There's only one sane way to approach this problem:

Code:
$ cat ydata
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBBB
BBBBBBYBBBBYBBYBBBBBYBBBB
BBBBBBYBBBBYBBYBYBBBBBBBB

$ i=0; IFS=Y; while read x; do set ${x:16:5}.; ((i+=$#-1)); done < ydata; echo $i
4

Smilie

Cheers,
Alister

Last edited by alister; 01-19-2010 at 09:18 PM.. Reason: changed variable name for readability
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