Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

how to count special characters?

SOLVED
Go to solution
Gemini_2
Regular Advisor

how to count special characters?

what is the shell command to find the number of the certain character

for example, I have a string

1,2,3,4,6

how do I find the number of "," in the string?

thank you

13 REPLIES
Pete Randall
Outstanding Contributor

Re: how to count special characters?

grep ',' string |wc -c


Pete

Pete
RAC_1
Honored Contributor
Solution

Re: how to count special characters?

echo 1,2,3,46 | tr -cd '\,' | wc -c
There is no substitute to HARDWORK
Pete Randall
Outstanding Contributor

Re: how to count special characters?

Forget that idea, it didn't work.


Pete

Pete
Highlighted
Kent Ostby
Honored Contributor

Re: how to count special characters?

If your data is in the file "inputfile", type:
awk '{FS=",";}{print $NF-2}' < inputfile

If there are no commas, it will issue a "-1 or -2" for that line.

For the line above, it will yield 4
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Jean-Luc Oudart
Honored Contributor

Re: how to count special characters?

Hi

try

A="a,b,c,d,e,"
echo $A | awk -v var1="," '{print gsub(var1," ");}'


Regards
Jean-Luc
fiat lux
Gemini_2
Regular Advisor

Re: how to count special characters?

thanks everyone for your speedy reply..

"echo 1,2,3,46 | tr -cd '\,' | wc -c"

is a very interesting use of "tr".

May I ask more questions?
can you explain more on "tr -cd"
-c is "first complement SET1", what does it mean?...
Sandman!
Honored Contributor

Re: how to count special characters?

The "-c" option tells tr what characters NOT to translate..."-c" (complement) option.

For example the following command makes the non-alphabetic characters in a file stand out by translating them to a number sign.

# tr -c '[A-Z][a-z]' '[#*]' < input_file


hope it helps!!!
Sandman!
Honored Contributor

Re: how to count special characters?

Pasted below is a C program that you can compile and execute for counting commas (or any character with slight modifications) within your input files.

==============================================================
#include
#define SPLCHR ','

main()
{
int nc, c;

while ((c = getchar()) != EOF)
if (c == SPLCHR)
++nc;
printf("Number of commas in the input string is: %d\n", nc);
}
==============================================================

cheers!
Arturo Galbiati
Esteemed Contributor

Re: how to count special characters?

Hi,
echo 1,2,3,4|awk -F, '{print NF-1}'

This counts the number of fields separted by ',' minus 1 giving you the number of the separtor.

HTH,
Art
H.Merijn Brand (procura
Honored Contributor

Re: how to count special characters?

*the* command to do so is 'freq' (if you have it). It's one of those lesser used pearls of the ol' unix times

a5:/u/usr/merijn 102 > freq .tcshrc
|nul 0| 1175|@ 6|` 34|
|soh 0|! 3|A 57|a 116|
|stx 0|" 104|B 8|b 75|
|etx 0|# 32|C 39|c 118|
|eot 0|$ 113|D 35|d 102|
|enq 0|% 42|E 99|e 415|
|ack 0|& 28|F 13|f 99|
|bel 2|' 66|G 8|g 25|
|bs 0|( 58|H 54|h 82|
|ht 192|) 60|I 37|i 207|
|lf 253|* 5|J 0|j 4|
|vt 0|+ 11|K 2|k 20|
|ff 0|, 27|L 45|l 138|
|cr 0|- 57|M 70|m 66|
|so 0|. 38|N 47|n 193|
|si 0|/ 199|O 80|o 137|
|dle 0|0 61|P 72|p 128|
|dc1 0|1 33|Q 3|q 3|
|dc2 0|2 21|R 76|r 171|
|dc3 0|3 19|S 77|s 312|
|dc4 0|4 6|T 93|t 278|
|nak 0|5 7|U 28|u 61|
|syn 0|6 12|V 8|v 79|
|etb 0|7 1|W 0|w 26|
|can 0|8 11|X 6|x 17|
|em 0|9 3|Y 14|y 37|
|sub 0|: 72|Z 1|z 1|
|esc 9|; 17|[ 9|{ 17|
|fs 0|< 4|\ 6|| 19|
|gs 0|= 163|] 8|} 17|
|rs 0|> 1|^ 14|~ 10|
|us 0|? 38|_ 25|del 0|
a5:/u/usr/merijn 103 >

freq also supports -a to show the 8-bit characters

Since many systems don't have it, I have included/attached the source code

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Muthukumar_5
Honored Contributor

Re: how to count special characters?

You can try as,

echo 1,2,3,4,6 | perl -ne 's/[^,]//g;print length,"\n";'

hth.
Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: how to count special characters?

Hi,
please, could you assign points?

0 point for this, of course

THX,
Art
Gemini_2
Regular Advisor

Re: how to count special characters?

After the first person replied me, I assigned the point right away and have not had visiting the forum for a while.

my apology. let me assign some points now.