Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-26-2012
Registered User
 
Join Date: Apr 2012
Posts: 33
Thanks: 28
Thanked 0 Times in 0 Posts
[Solved] Count amount of times of appearing of character before a word?

Hello Is there a way to calculate how many times a particular symbol appeared in a string before a particular word.

Code:
 Desktop/Myfiles/pet/dog/puppy

So, I want to count number of occurence of"/" in this directory before the word dog lets say.
Cheers,

Bob
Sponsored Links
    #2  
Old 04-26-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts
echo "Desktop/Myfiles/pet/dog/puppy" | awk -F\/ '{for(i=1;i<=NF;i++){if($i~/dog/)print i-1}}'
The Following User Says Thank You to itkamaraj For This Useful Post:
FUTURE_EINSTEIN (04-26-2012)
Sponsored Links
    #3  
Old 04-26-2012
Registered User
 
Join Date: Oct 2007
Location: USA
Posts: 1,299
Thanks: 11
Thanked 99 Times in 95 Posts

Code:
echo "Desktop/Myfiles/pet/dog/puppy" | awk -F"dog" '{print split($1,a,"/")-1}'

The Following User Says Thank You to shamrock For This Useful Post:
FUTURE_EINSTEIN (04-26-2012)
    #4  
Old 04-26-2012
Registered User
 
Join Date: Apr 2012
Posts: 33
Thanks: 28
Thanked 0 Times in 0 Posts
cheers!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to count number of times each word exist in a file shnkool UNIX for Dummies Questions & Answers 4 12-07-2011 04:54 AM
Count specific word or character per line janzper UNIX for Advanced & Expert Users 1 05-27-2011 02:42 AM
How to format a character to look like an amount -solved ryukishin_17 Shell Programming and Scripting 0 04-12-2010 12:13 AM
scripting - write a script that will count the number of times a particular word BigTool4u2 Shell Programming and Scripting 3 06-24-2009 01:46 PM
Looking for a single line to count how many times one character occurs in a word... Shingoshi Shell Programming and Scripting 2 06-12-2009 10:12 PM



All times are GMT -4. The time now is 11:26 AM.