How to count no of occurences of a character in a string in UNIX


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to count no of occurences of a character in a string in UNIX
# 1  
Old 03-15-2006
Error How to count no of occurences of a character in a string in UNIX

i have a string like echo "a|b|c" . i want to count the | symbols in this string . how to do this .plz tell the command
# 2  
Old 03-15-2006
Code:
echo $((`echo "a|b|c" | sed 's/[^|]//g' | wc -c` - 1 ))

# 3  
Old 03-15-2006
Similar, but a bit different:
Code:
echo $(($(echo "a|b|c"|sed 's/[a-z]//g'|wc -c)-1))

# 4  
Old 03-15-2006
Quote:
echo $(($(echo "a|b|c"|sed 's/[a-z]//g'|wc -c)-1))
if the above is the solution,
the input is restricted to characters from a-z delimited by '|'
if the input includes numbers,
the solution should include numbers also and similiarly for special characters.

Hence, retaining '|' characters would be easier to maintain.
# 5  
Old 03-15-2006
Thank s

Hey madhan ,

thanks for sending the aboove command to count the no of times a character occurred. its working fine.

but i found a simple way of doing this.
Just count the no of fields between each | symbol and subtract 1 from the total count

the code is:

no_fields=`echo "a|b|c " |awk -F"|" '{print NF}' `
echo $no_fields

it will return 3 and subtract 1 from it to get answer as 2

it will work for string of any length
# 6  
Old 03-15-2006
Quote:
no_fields=`echo "a|b|c " |awk -F"|" '{print NF}' `
echo $no_fields
substract it directly then,
no subsequent step needed
Code:
echo "a|b|c " |awk -F"|" '{print NF-1}'

# 7  
Old 03-17-2006
Probably the simplest way is:

Code:
echo "a|b|c" | tr -dc '|' | wc -c

The 'tr' command simply deletes any input character that is not a '|' leaving you with just the character you want to count.

Similar discussion is here

Unbeliever
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count occurences of a character in a file by sorting results

Hello, I try to sort results of occurences in an array by using awk but I can't find the right command. that's why I'm asking your help ! :) Please see below the command that I run: awk '{ for ( i=1; i<=length; i++ ) arr++ }END{ for ( i in arr ) { print i, arr } }' dictionnary.txt ... (3 Replies)
Discussion started by: destin45
3 Replies

2. Shell Programming and Scripting

Character count in UNIX!

Hi, I am Beginner in writing shell scripting. I have tried to get the character count using wc command. But it is not giving the correct result. Could any one please tell me the reason? $ cat k.ksh Shell scripting The character count should be 15 but it is displaying as 16 when i use... (8 Replies)
Discussion started by: nikesh29
8 Replies

3. Shell Programming and Scripting

Awking string only 6 character long and providing a count

Morning Guys, I am attempting to awk a file which strings in the file is only 6 characters long and not more. Currently it is counting every line and giving a count of 59, but it should be 57 (not including the long baracode - 004705CIM*****) " awk '/./ {cnt++} END {print cnt}'... (11 Replies)
Discussion started by: Junes
11 Replies

4. UNIX for Advanced & Expert Users

couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '' | wc -c using this command i can count the no of times a number from 0-9 occurs in the string "hello123" but how do i save this result inside a variable? if i do x= echo "hello123" | tr -dc '' | wc -c that does not work...plz suggest..thanks (3 Replies)
Discussion started by: arindamlive
3 Replies

5. Shell Programming and Scripting

Count occurences of a numeric string falling in a range

Dear all, I have numerous dat files (1.dat, 2.dat...) containing 500 numeric values each. I would like to count them, based on their range and obtain a histogram or a counter. INPUT: 1.dat 1.3 2.16 0.34 ...... 2.dat 1.54 0.94 3.13 ..... ... (3 Replies)
Discussion started by: chen.xiao.po
3 Replies

6. Shell Programming and Scripting

Count occurences of string

Hi, Please help me in finding the number of occurences of the string. Example: Apple, green, blue, Apple, Orange, green, blue are the strings can be even in the next line. The o/p should look as: Word Count ----- ----- Apple 2 green 2 Orange 1 blue 2 Thanks (2 Replies)
Discussion started by: acc888
2 Replies

7. Shell Programming and Scripting

awk: sort lines by count of a character or string in a line

I want to sort lines by how many times a string occurs in each line (the most times first). I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass). However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies

8. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

9. Shell Programming and Scripting

delete last character in all occurences of string

Hello all, I have a file containing the following p1 q1 p2 q2 p1 p2 p3 pr1 pr2 pr1 pr2 pa1 pa2 I want to remove the last character from all strings that start with 'p' and end with '1'. In general, I do not know what is between the first part of the string and the last part of the string.... (4 Replies)
Discussion started by: bigfoot
4 Replies

10. HP-UX

count occurences of specific character in the file

For counting the occurences of specific character in the file I am issuing the command grep -o 'character' filename | wc -w It works in other shells but not in HP-UX as there is no option -o for grep. What do I do now? (9 Replies)
Discussion started by: superprogrammer
9 Replies
Login or Register to Ask a Question