Delimiter count in a string. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Delimiter count in a string.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-20-2008
priyam priyam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 29 September 2009, 5:03 AM EDT
Location: India
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Question Delimiter count in a string.

Hi ,

I need to get the delimiter "-" count in a particular string.

string= SYS_NAME-123-S5-2008-10-20.LOG

the delimit "-" count is 5 .

Using sed or awk can I know the count ?

I have seen how to get the count for delimiter in a file but not a string

Thanks,
Priya
Sponsored Links
    #2  
Old 10-20-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 31 August 2014, 5:38 PM EDT
Location: NM
Posts: 10,180
Thanks: 275
Thanked 785 Times in 734 Posts

Code:
echo "$mystring" | awk -F'-' '{ print NF }'

Sponsored Links
    #3  
Old 10-20-2008
priyam priyam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 29 September 2009, 5:03 AM EDT
Location: India
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
but this is showing output as 6 instead of 5 ?Why is that so ?
    #4  
Old 10-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 26 August 2014, 2:14 PM EDT
Location: Out running a Marathon.
Posts: 2,322
Thanks: 71
Thanked 153 Times in 142 Posts
One approach in unix


Code:
> echo `echo "SYS_NAME-123-S5-2008-10-20.LOG" | tr "-" "\n" | wc -l` -1 | bc
5

Sponsored Links
    #5  
Old 10-20-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 1 September 2014, 10:03 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,667
Thanks: 182
Thanked 617 Times in 575 Posts
Or:


Code:
string=SYS_NAME-123-S5-2008-10-20.LOG ifs="$IFS" IFS=-
set -- $string; count=$(($#-1));IFS="$ifs"

The variable count contains the number of -'s.

With zsh:


Code:
count=$((${#${(s.-.)string}}-1))


Last edited by radoulov; 10-20-2008 at 10:26 AM..
Sponsored Links
    #6  
Old 10-20-2008
ranjithpr ranjithpr is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 20 August 2010, 3:34 AM EDT
Posts: 175
Thanks: 0
Thanked 3 Times in 3 Posts
echo "SYS_NAME-123-S5-2008-10-20.LOG"|awk -F'-' '{ print NF-1 }'
Sponsored Links
    #7  
Old 10-20-2008
priyam priyam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 29 September 2009, 5:03 AM EDT
Location: India
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks

The above methods are working ... Thanks
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 cut all string after the last delimiter? sunnydanniel Shell Programming and Scripting 3 02-28-2012 07:37 PM
How to split a string with no delimiter saint34 Shell Programming and Scripting 8 03-01-2011 02:27 PM
Count delimiter and replace dsubha Shell Programming and Scripting 7 10-25-2010 05:48 AM
creating a delimiter string satish@123 Shell Programming and Scripting 0 05-21-2008 05:38 AM
split string with multibyte delimiter azmathshaikh Shell Programming and Scripting 1 03-06-2005 05:50 AM



All times are GMT -4. The time now is 05:06 PM.