How to find no of underscores in a variable? | 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.

How to find no of underscores in a variable?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-13-2013
pracheth pracheth is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 19 December 2013, 11:59 PM EST
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
How to find no of underscores in a variable?

Hi
i have a variable var=a_b_c
i want command to find no. of underscores in a variable

Thank you
Sponsored Links
    #2  
Old 09-13-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 29 July 2014, 2:02 PM EDT
Location: NM
Posts: 10,156
Thanks: 269
Thanked 781 Times in 731 Posts
one way:

Code:
a=$(echo "$var" | tr -d '[:alpha:]' )
echo ${#a}

Sponsored Links
    #3  
Old 09-13-2013
targetshell targetshell is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 6 November 2013, 4:53 AM EST
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jim mcnamara View Post
one way:

Code:
a=$(echo "$var" | tr -d '[:alpha:]' )
echo ${#a}


Hi can you explain the usages of each command that you used.

What tr -d does?
'[:alpha:]'
??
    #4  
Old 09-13-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 22 July 2014, 6:56 AM EDT
Posts: 328
Thanks: 35
Thanked 86 Times in 85 Posts
another way to do it is by using awk:

Code:

echo a_b_c | awk -F_ '{print NF-1}'

Sponsored Links
    #5  
Old 09-13-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
Quote:
Originally Posted by targetshell View Post
Hi can you explain the usages of each command that you used.

What tr -d does?
'[:alpha:]'
??

Code:
tr -d list

Delete the characters found in the list from the input stream.

Code:
[:alpha:]

A special character class. In this case instead of listing all the letters from a to z and A to Z use this shorthand. A list of these can be found here

Andrew
Sponsored Links
    #6  
Old 09-13-2013
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
Quote:
Originally Posted by jim mcnamara View Post

Code:
tr -d '[:alpha:]'

To count the number of underscores, the most robust solution deletes everything that isn't an underscore.

Code:
tr -cd _

Regards,
Alister
Sponsored Links
    #7  
Old 09-13-2013
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 29 July 2014, 4:21 PM EDT
Location: East Coast
Posts: 602
Thanks: 1
Thanked 172 Times in 158 Posts
Using pure Bash (straight, no chaser) :


Code:
var="a_b_c"
count="${var//[^_]}"
echo "${#count}"
2

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
No. of underscores in a file name pracheth Shell Programming and Scripting 10 09-14-2013 12:27 AM
extract string from file name between two underscores 1988PF Shell Programming and Scripting 7 07-19-2012 03:02 PM
Scripting to convert underscores to spaces brakeb Shell Programming and Scripting 6 10-17-2008 02:23 PM
Find a environment variable Value ! tkbharani UNIX for Advanced & Expert Users 1 07-06-2007 08:01 AM
change spaces to underscores script !!! piltrafa UNIX for Dummies Questions & Answers 2 06-05-2002 08:21 PM



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