Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Delimiter count in a string.

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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
Unix or Linux 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 Linux

Thanks,
Priya
Sponsored Links
    #2  
Old Unix and Linux 10-20-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 6 July 2015, 11:34 AM EDT
Location: NM
Posts: 10,496
Thanks: 344
Thanked 869 Times in 807 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 10-20-2008
joeyg's Unix or Linux Image
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 22 June 2015, 10:58 AM EDT
Location: Within two miles of a Dunkin donuts.
Posts: 2,356
Thanks: 85
Thanked 163 Times in 149 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 Unix and Linux 10-20-2008
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 625 Times in 583 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 Unix and Linux 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 Unix and Linux 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 ... ThanksLinux
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 02:47 AM.