UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 2nd, 2005, 06:03 AM
mikie7boy mikie7boy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 97 mikie7boy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 1 m 4 sec
Reputation Power: 6
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

Reply With Quote
  #2  
Old April 2nd, 2005, 10:48 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 44 m 45 sec
Reputation Power: 9
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.

Reply With Quote
  #3  
Old April 4th, 2005, 04:44 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Quote:
Originally Posted by guggach
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



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

Reply With Quote
  #4  
Old April 4th, 2005, 04:58 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Quote:
Originally Posted by zlutovsky
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


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("/", "")}'




Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > counting number of times / appears in a string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway