|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
counting number of times / appears in a string
Hello people,
I need to find out how many times the "/" character appears in a given string variable. For example: myPath=./main/documents/test.txt I need abit of code to tell me that $myPath has 3 "/" characters. If this is possible could someone tell me how to do it or point me in the right direction. Cheers mike |
|
#2
|
|||
|
|||
|
no *nix standard prog does it.
if you can write C or perl, it's a 5 lines code, when you get probls, post again ![]()
__________________
working on Solaris[5-9], preferred languages french and C. |
|
#3
|
|||
|
|||
|
Quote:
I propose a shell script solution: : A=/sss/ddd/sdfg B=$( echo $A | tr -d "/" ) # String without slashes LA=$( expr $A : '.*' ) # length of A LB=$( expr $B : '.*' ) LEN=$( expr $LA - $LB ) echo $LEN long, but it is a possibility. C or perl solution would be more elegant. Regards ![]() |
|
#4
|
|||
|
|||
|
Quote:
Here is a shorter version for ksh or bash: : A=/sss/ddd/sdfg B=$( echo $A | tr -d "/" ) # String without slashes let LEN=${#A}-${#B} echo $LEN and here two even shoter versions: : A=/sss/ddd/sdfg// B=$(echo $A | sed 's/[^/]//g') echo ${#B} echo $A | awk '{print gsub("/", "")}' ![]() |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > counting number of times / appears in a string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|