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.

How to find no of underscores in a variable?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 09-13-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 June 2015, 4:49 PM EDT
Location: NM
Posts: 10,495
Thanks: 344
Thanked 869 Times in 807 Posts
one way:

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

Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 09-13-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 7 May 2015, 3:00 AM EDT
Posts: 332
Thanks: 37
Thanked 87 Times in 86 Posts
another way to do it is by using awk:

Code:

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

Sponsored Links
    #5  
Old Unix and Linux 09-13-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 10 November 2014, 4:46 AM EST
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 Unix and Linux 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 974 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 Unix and Linux 09-13-2013
in2nix4life's Unix or Linux Image
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 11 June 2015, 2:00 PM EDT
Location: East Coast
Posts: 620
Thanks: 1
Thanked 177 Times in 163 Posts
Using pure Bash (straight, no chaser) Linux:


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

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
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 08:02 PM.