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

No. of underscores in a file name

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
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 09-13-2013
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 21 October 2014, 2:41 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 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 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 09-13-2013
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 21 October 2014, 2:41 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 Posts

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

Sponsored Links
    #5  
Old 09-13-2013
wisecracker's Avatar
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 23 October 2014, 2:33 AM EDT
Location: Loughborough
Posts: 640
Thanks: 133
Thanked 136 Times in 113 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 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 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 09-13-2013
wisecracker's Avatar
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 23 October 2014, 2:33 AM EDT
Location: Loughborough
Posts: 640
Thanks: 133
Thanked 136 Times in 113 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 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
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 10:26 AM.