Need help to find total counts in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to find total counts in a string
# 1  
Old 08-01-2014
Need help to find total counts in a string

Suppose I have a string " 1 A B C D F 3 F G 3 1 D L D ".

Please let me know the command which tells the above string having 3 Ds.

echo $str | grep -c "D"

It returns only 1 I know the reason but I am looking some command which should give 3.

Actually I have a file, which I need to read and store the total numbers of ":" in each record.

Any help is highly appreciated.
# 2  
Old 08-01-2014
Code:
echo "1 A B C D F 3 F G 3 1 D L D" | grep -o "D" | wc -l

Code:
echo "1 A B C D F 3 F G 3 1 D L D" | tr ' ' '\n' | grep -c "D"

Code:
x="1 A B C D F 3 F G 3 1 D L D"
y=${x//[^D]}
echo ${#y}

# 3  
Old 08-01-2014
Try
Code:
echo " 1 A B C D F 3 F G 3 1 D L D " | tr -cd "D"| wc -c

# 4  
Old 08-01-2014
THANK YOU !

Code:
echo " 1 A B C D F 3 F G 3 1 D L D " | tr -cd "D"| wc -c

Worked perfectly...

grep -o won't work in HP Unix...Sorry I forgot to mention the Unix flavor...

Again thanks for all your help !




Moderator's Comments:
Mod Comment Please use CODE tags!


---------- Post updated at 07:12 AM ---------- Previous update was at 07:04 AM ----------

But could you please explain the meaning of the command...

I tried to understand (man tr) but didn't get it.
Code:
echo " 1 A B C D F 3 F G 3 1 D L D " | tr -cd "D"

It returns nothing....please explain, if possible.

Last edited by rbatte1; 08-01-2014 at 09:05 AM.. Reason: Added CODE tags
# 5  
Old 08-01-2014
-d deletes characters which match the specified set, -c modifies it to use the complement of the set instead (anything which isn't D).
# 6  
Old 08-01-2014
Code:
akshay@nio:~$ echo " 1 A B C D F 3 F G 3 1 D L D " | awk '{print gsub(search,"&")}' search='D'
3

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Find the total size of multiple files

If I have a number of files in a directory, for example, test.1 test.2 test.3 abc.1 abc.2 abc.3 and I need to find the total file size of all of the test.* files, I can use du -bc test.* in Linux. However, in Solaris, du does not have the -c option. What can I do in Solaris to get... (11 Replies)
Discussion started by: learnix
11 Replies

2. Shell Programming and Scripting

Shell script to search a pattern in a directory and output number of find counts

I need a Shell script which take two inputs which are 1) main directory where it has to search and 2) pattern to search within main directory all files (.c and .h files) It has to print number of pattern found in main directory & each sub directory. main dir --> Total pattern found = 5 |... (3 Replies)
Discussion started by: vivignesh
3 Replies

3. Shell Programming and Scripting

How to find the average,min,max ,total count?

Hi , Below is my sample data,I have this 8 column(A,B,C,D,E,F,G,H) in csv file. A , B ,C ,D ,E ,F,G ,H 4141,127337,24,15,20,69,72.0,-3 4141,128864,24,15,20,65,66.0,-1 4141,910053,24,15,4,4,5.0,-1 4141,910383,24,15,22,3,4.0,-1 4141,496969,24,15,14,6,-24.0,-18... (7 Replies)
Discussion started by: vinothsekark
7 Replies

4. Shell Programming and Scripting

Command to find total cpu and mem

Hi All, I need to know the command for knowing the total cpu and mem. Thanks (5 Replies)
Discussion started by: aish11
5 Replies

5. Shell Programming and Scripting

Search and find total count from multiple files

Please advice how can we search for a string say (abc) in multiple files and to get total occurrence of that searched string. (Need number of records that exits in period of time). File look like this (read as filename.yyyymmdd) a.20100101 b.20100108 c.20100115 d.20100122 e.20100129... (2 Replies)
Discussion started by: zooby
2 Replies

6. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

7. Solaris

Solaris 10 - How to find the total size of my hard disk?

Friends, I have an 80 GB HDD, but I wish to know if there is a direct command in Solaris 10 to find out the size of my hard disk (similar to fdisk -l in Linux). Thank you saagar (3 Replies)
Discussion started by: saagar
3 Replies

8. UNIX for Dummies Questions & Answers

Find total size for some files?

Hi, I'm newbie to Unix. I'd like to count the total size of those files in my directory by date. For example, files on this period 05/01/08 - 05/31/08. If possible can we count by byte instead of kb. if I use $ du - ks , it will add up all files in the dir. thanks, Helen (5 Replies)
Discussion started by: helen008
5 Replies

9. Filesystems, Disks and Memory

How to find the total size of a dirctory tree in Solaris

Hi, I want to find the total size of some directory trees in my solaris 9 machine. Is there a command or utility I can use to do it. Please let me know if there is any way. Thanks Akheel (1 Reply)
Discussion started by: 0ktalmagik
1 Replies
Login or Register to Ask a Question