Number of specific char in a string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number of specific char in a string.
# 1  
Old 12-17-2003
Data Number of specific char in a string.

I wish to compute the number of dot chars in a string.
Example:
VAR="aaaa.bbbbb.cccc"

I try the shortest command to solve this test.

Thanks in advance for your help.
Regards,

Giovanni
# 2  
Old 12-17-2003
echo $VAR|awk '{print gsub("\.",0)}'
# 3  
Old 12-17-2003
As far as I am aware the gsub trick only works with gawk.
The AT&T version (awk/nawk) does not return the number
of substitutions. Nor is this required by POSIX or Single
UNIX Specifications.

Another way of achieving the required result is:

echo $VAR | tr -c -d '.' | wc -c

- Finnbarr
# 4  
Old 12-18-2003
The gsub() function exists in the freely available awks including POSIX awk, nawk and gawk.

Nice solution though.
# 5  
Old 12-19-2003
Yes, gsub() exists in all versions of g/n/awk. However, gawk
is the only version where the man page states that the
function returns the number of substitutions made.

Man pages for the other versions of awk are silent on this
issue. POSIX.1:2003 does not mandate it. This being the
case, one cannot rely on this behavour in portable code.

- Finnbarr
# 6  
Old 12-19-2003
Thanks!Smilie

On my OS (Aix v. 5.1) the command echo $VAR|awk '{print gsub("\.",0)}' returns always 1 if the VAR string contains or not a dot character.

echo $VAR|awk '{print gsub("\.",0)}' works correctly.

Giovanni
# 7  
Old 12-19-2003
echo ${VAR}|awk -F. '{print NF-1}'
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

2. UNIX for Dummies Questions & Answers

Remove char if not a number

I need to write a BASH script that takes a 2 character string and removes the second character if it is not a digit e.g. If the string is numberical value >9 e.g. string1 = '34' then leave string1 = '34'. However if the string is <10 e.g. string1 = '3X' then remove the second char (which... (7 Replies)
Discussion started by: millsy5
7 Replies

3. Shell Programming and Scripting

How count number of char?

hello how can i cont number of char with loop coomand? i dont want to use wc or other special command the script should check all word's char. one by one also a counter can handle the number As noted in other threads started today. This is not the correct forum for homework assignments. ... (2 Replies)
Discussion started by: nimafire
2 Replies

4. Shell Programming and Scripting

Remove only specific char on every line when exists

Hi I need to remove "|" char when it's the last char of the line. Input file: generoso|desprendido|altruista| abnegar|ceder|sacrificar| abocetado-da|esbozado| apuntado|insinuado|incompleto abocetar|esbozar|bosquejar| diseņar|delinear ------------------------ output need --- ... (11 Replies)
Discussion started by: lookoo
11 Replies

5. Shell Programming and Scripting

Checking variable with specific string and stripping number from it.

I am parsing a file and I get differnt results everytime. Sometimes I get 12s sometimes I get 54m and sometime 3h.. v1=12s or v1=54m or v1=3h 12s - 12 seconds 54m - 54 minutes 3h - 3 hour I have to write a script in such a way that it whenever v1 is in minutes, I should strip "m"... (14 Replies)
Discussion started by: jayeshpatel
14 Replies

6. Shell Programming and Scripting

To find number of char occur

To find out number of "|" symbol is available in file: Input: a|b|c|d|z Ouput: 4 I am using below set of commands,It is working... Anybody have anyother solution using sed / awk. cnt=`wc -c <1.txt` cnt1=`tr -d "|" <1.txt >c.dat` cnt2=`wc -c <c.dat` outp=`expr $cnt... (19 Replies)
Discussion started by: Jairaj
19 Replies

7. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

8. Shell Programming and Scripting

how to get number char from a string

for example: i hav a string like : /rmsprd/arch01/rmsprd/rmsprdarch72736.log how I can extract my_num=72736? I know I can echo "/rmsprd/arch01/rmsprd/rmsprdarch72736.log" | tr "/" " " | awk '{ print $4 }' to get rmsprdarch72736.log (4 Replies)
Discussion started by: netbanker
4 Replies

9. Shell Programming and Scripting

last char from a string

i have a script that reads a plain text file. (its a ksh, and i can use bash also) each line of the file is a fullpath of a file. that makes the list huge. i need to add a functionalitie to that script, i have to be able to add /usr/* or /usr/ and with that reference all the files and folders... (6 Replies)
Discussion started by: broli
6 Replies
Login or Register to Ask a Question