tell me how many specific characters there are?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting tell me how many specific characters there are?
# 1  
Old 03-16-2006
tell me how many specific characters there are?

i'm trying to get the user to enter a character, then the script should search for how many of that character exists in the file. I have the following code, but it doesn't work properly and it shows the wrong amount (i don't think im supposed to use grep). For example, I want it to say, "There are 7 'e' characters in the file", or "There are 5 'n' characters in the file" etc...

Thanks for your help.

echo
echo "Enter the Character to Search for:"
read char
grep -c $char text
echo
echo "Press any key to continue"
read key
# 2  
Old 03-16-2006
grep -c counts the number of lines in which the character appears.

I think the best thing to do would be to store the result of grep $char text.
Then, parse through each line of this result and count the occurence of the required character. Could get very tedious.

Or much better yet, use awk. Your field separator should be the desired character and then use the value NF.

Last edited by vino; 03-16-2006 at 12:47 PM..
# 3  
Old 03-16-2006
I'm sure one could do this with a little bit of shell scripting
but Perl's transliterate operator involves less effort.

e.g. if I was interested in the No. of slashes in my passwd I could say

# perl -ne '$c+=$_=~tr|/|/|;END{print"$c\n"}' /etc/passwd
184
# 4  
Old 03-16-2006
One way is to use tr -dc to delete anything that is not the character, then wc -c to count....
Code:
tr -dc $char < infile | wc -c

# 5  
Old 03-17-2006
Code:
grep -o $char infile | wc -l

# 6  
Old 03-17-2006
If you have access to a GNU grep that will work.
However, grep implementations on most Unices lack the -o switch.
Besides, I would prefer the solution posted by Ygor since a tr
should be far more efficient (you could try benchmarking both methods).
At the moment when I posted the Perl tr stuff I completely forgot
about the complement switch.
So undoubtably Ygor's is far more elegant.
One should more often look at the manpages of those text processing utilities like tr.
# 7  
Old 03-17-2006
Try this

Hi ,

try this...program has not takes care of Blank characters

echo " Enter the File name"
read file
cnt=`wc -c < $file`
echo "Enter Character to be searched"
read char
ans=` cat $file | tr -d [$char] `
ans=`echo $ans | wc -c`
act_cnt=`expr $cnt - $ans`
echo " Their are $act_cnt '$char' characters in file $file"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing specific characters using sed

Hi, I have a text file which is output from a server and it lists all the files in a specific volume. However, the volume name appears as volume_name:. I would like to replace this with \\volume_name\volume_name. This is not a problem in itself as I can use sed to globally look for the... (8 Replies)
Discussion started by: vnayak
8 Replies

2. Shell Programming and Scripting

Count specific characters at specific column positions

Hi all, I need help. I have an input text file (input.txt) like this: 21 GTGCAACACCGTCTTGAGAGG 50 21 GACCGAGACAGAATGAAAATC 73 21 CGGGTCTGTAGTAGCAAACGC 108 21 CGAAAAATGAACCCCTTTATC 220 21 CGTGATCCTGTTGAAGGGTCG 259 Now I need to count A/T/G/C numbers at each character location in column... (2 Replies)
Discussion started by: thienxho
2 Replies

3. Shell Programming and Scripting

Can't figure out how to find specific characters in specific columns

I am trying to find a specific set of characters in a long file. I only want to find the characters in column 265 for 4 bytes. Is there a search for that? I tried cut but couldn't get it to work. Ex. I want to find '9999' in column 265 for 4 bytes. If it is in there, I want it to print... (12 Replies)
Discussion started by: Drenhead
12 Replies

4. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

5. Shell Programming and Scripting

Generate specific amount of same characters

Hi, Is there a command to print one character x amont of times? I need for example 10 comma's (,,,,,,,,,,). Instead of creating a loop, I was wondering if there is a way to do this with sed or awk? Thanks! (3 Replies)
Discussion started by: Subbeh
3 Replies

6. Shell Programming and Scripting

Deleting values with specific characters

I have a file with 3 columns 2 4 5 2 4 7 3 5 7 4 -6 9 5 -9 4 6 -3 3 Bascially I want to delete the entire row if column 2 is a "-" So the end result will be 2 4 5 2 4 7 3 5 7 I have trouble doing this cause of the - in front of the number. thanks (2 Replies)
Discussion started by: kylle345
2 Replies

7. Shell Programming and Scripting

Removing all characters on and before specific point

Im having a file with records DB1635 |Y|N|DB1632 |000024968_202 |0|000024968302|RCF02| DB1636 |Y|N|DB1633 |000024968_203 |0|000024968302|RCF02| i want to get output as Y|DB1632 |RCF02| Y|DB1633 |RCF02| how can i do this ?? any... (3 Replies)
Discussion started by: Trendz
3 Replies

8. Shell Programming and Scripting

replacing specific characters in a string

Hi Friends, Following is an output of a script OPWQERIUTYKLSADFJHGXZNMCVBWQOPERIUTYKLSADFJHGXZNMCVB I want to replace above string's 11 to 17 character by ******* (7 stars) How can it be done? Please somebody guide me. (6 Replies)
Discussion started by: anushree.a
6 Replies

9. AIX

Delete specific characters

Hi every1 Well i have a list of numbers e.g 12304 13450 01234 00123 14567 what i want is a command to check if the number is starting from 0 and then delete the 0 without doing anything else!!!! any help wud b appreciated!!!!!!!!:( (4 Replies)
Discussion started by: masquerer
4 Replies

10. Shell Programming and Scripting

Terminal-specific characters in password

Good morning! I am using a shell script to back up user email files to a remote location. The problem is, one (and apparently more than one) users have the symbol "@" in their password. As you can see from the line: /sbin/mount_smbfs... (5 Replies)
Discussion started by: PittWolfBW
5 Replies
Login or Register to Ask a Question