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 online now Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 24 November 2014, 10:49 AM EST
Location: NM
Posts: 10,281
Thanks: 288
Thanked 814 Times in 756 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: 17 November 2014, 3:07 PM EST
Location: Out running a Marathon.
Posts: 2,337
Thanks: 73
Thanked 155 Times in 144 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: 24 November 2014, 9:39 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 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 11: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 08:37 PM
How to split a string with no delimiter saint34 Shell Programming and Scripting 8 03-01-2011 03:27 PM
Count delimiter and replace dsubha Shell Programming and Scripting 7 10-25-2010 06:48 AM
creating a delimiter string satish@123 Shell Programming and Scripting 0 05-21-2008 06:38 AM
split string with multibyte delimiter azmathshaikh Shell Programming and Scripting 1 03-06-2005 06:50 AM



All times are GMT -4. The time now is 12:02 PM.