Re: how to count special characters? (167 Views)
Reply
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 1 of 14 (168 Views)
Accepted Solution

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

Please use plain text.
Outstanding Contributor
Pete Randall
Posts: 16,201
Registered: ‎11-03-1996
Message 2 of 14 (168 Views)

Re: how to count special characters?

grep ',' string |wc -c


Pete

Pete
Please use plain text.
Honored Contributor
RAC_1
Posts: 5,920
Registered: ‎03-21-2002
Message 3 of 14 (169 Views)

Re: how to count special characters?

echo 1,2,3,46 | tr -cd '\,' | wc -c
There is no substitute to HARDWORK
Please use plain text.
Outstanding Contributor
Pete Randall
Posts: 16,201
Registered: ‎11-03-1996
Message 4 of 14 (167 Views)

Re: how to count special characters?

Forget that idea, it didn't work.


Pete

Pete
Please use plain text.
Honored Contributor
Kent Ostby
Posts: 1,836
Registered: ‎09-26-2000
Message 5 of 14 (167 Views)

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"
Please use plain text.
Honored Contributor
Jean-Luc Oudart
Posts: 1,461
Registered: ‎03-10-1998
Message 6 of 14 (167 Views)

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
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 7 of 14 (167 Views)

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?...
Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 8 of 14 (167 Views)

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!!!
Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 9 of 14 (167 Views)

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!
Please use plain text.
Esteemed Contributor
Arturo Galbiati
Posts: 830
Registered: ‎02-10-2003
Message 10 of 14 (167 Views)

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
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 11 of 14 (167 Views)

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
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 12 of 14 (167 Views)

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!
Please use plain text.
Esteemed Contributor
Arturo Galbiati
Posts: 830
Registered: ‎02-10-2003
Message 13 of 14 (167 Views)

Re: how to count special characters?

Hi,
please, could you assign points?

0 point for this, of course

THX,
Art
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 14 of 14 (167 Views)

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.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation