Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Delimiter count in a string.

Shell Programming and Scripting


Closed    
 
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: 30 November 2016, 10:00 AM EST
Location: NM
Posts: 10,874
Thanks: 457
Thanked 984 Times in 915 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: 29 November 2016, 1:38 PM EST
Location: Within two miles of a Dunkin donuts.
Posts: 2,413
Thanks: 102
Thanked 177 Times in 163 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 Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 16 December 2015, 5:59 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 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 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

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 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 03:49 PM.