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.

No. of underscores in a file name

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
No. of underscores in a file name

Hi

I want to filter my files which has more than 1 underscores in it.how can i achieve this.
Ex: if my file name is a_b_c, a_b.
my command should result only a_b_c

Thanks
Pracheth
Sponsored Links
    #2  
Old Unix and Linux 09-13-2013
bartus11's Unix or Linux Image
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 June 2015, 6:17 PM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 Posts
Try:
Code:
find . -name "*_*_*"

The Following User Says Thank You to bartus11 For This Useful Post:
pracheth (09-13-2013)
Sponsored Links
    #3  
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
Hi
Thank you for your quick response.
Adding to that..how can i find whether a particular variable has two underscores in it.
suppose var=a_b_c
i want to know if var has two underscores or not
    #4  
Old Unix and Linux 09-13-2013
bartus11's Unix or Linux Image
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 June 2015, 6:17 PM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 Posts

Code:
echo "$var" | grep ".*_.*_.*" > /dev/null && echo "$var has two or more underscores"

Sponsored Links
    #5  
Old Unix and Linux 09-13-2013
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 27 June 2015, 11:56 AM EDT
Location: Loughborough
Posts: 810
Thanks: 186
Thanked 182 Times in 149 Posts
A longhand version using an OSX 10.7.5 terminal...

Code:
Last login: Fri Sep 13 12:52:40 on ttys000
AMIGA:barrywalker~> text="a_b_c"; count=0; for n in $( seq 0 1 ${#text} ); do if [ "${text:$n:1}" == "_" ]; then count=$[ ( $count + 1 ) ]; fi; done; echo""; echo "Underscores = $count"

Underscores = 2
AMIGA:barrywalker~> _

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 wisecracker View Post
Code:
text="a_b_c"; count=0; for n in $( seq 0 1 ${#text} ); do if [ "${text:$n:1}" == "_" ]; then count=$[ ( $count + 1 ) ]; fi; done; echo""; echo "Underscores = $count"

If your shell supports substring expansion ( ${text:$n:1} ), then you can probably replace all of that with a much simpler, pattern substitution alternative:

Code:
u=${text//[!_]/}
echo Underscores = ${#u}

Regards,
Alister
The Following User Says Thank You to alister For This Useful Post:
wisecracker (09-13-2013)
Sponsored Links
    #7  
Old Unix and Linux 09-13-2013
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 27 June 2015, 11:56 AM EDT
Location: Loughborough
Posts: 810
Thanks: 186
Thanked 182 Times in 149 Posts
Hi alister...

Thanks for that one...

I read it that the OP wanted a number that can be used for detecting further in a script.
Yours shortening works except I changed your "u" variable for count, but...

Code:
Last login: Fri Sep 13 19:30:46 on ttys000
AMIGA:barrywalker~> text="a_b_c"
AMIGA:barrywalker~> count=${text//[!_]/}
AMIGA:barrywalker~> echo "$count"
__
AMIGA:barrywalker~> echo ${#count}
2
AMIGA:barrywalker~> count=$(echo -n ${#count})
AMIGA:barrywalker~> echo "$count"
2
AMIGA:barrywalker~> _

Re-using the "count" variable...
Code:
count=$(echo -n ${#count})

...keeps the variables to two including the "text" test variable otherwise there would have to be a third variable. The "count" variable is now a number...
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
Match list of strings in File A and compare with File B, C and write to a output file in CSV format asnandhakumar Shell Programming and Scripting 6 10-11-2012 03:47 PM
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
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 11:30 PM.