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
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
Try:
find . -name "*_*_*"
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
echo "$var" | grep ".*_.*_.*" > /dev/null && echo "$var has two or more underscores"
A longhand version using an OSX 10.7.5 terminal...
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~> _
If your shell supports substring expansion ( ${text:$n:1}
), then you can probably replace all of that with a much simpler, pattern substitution alternative:
u=${text//[!_]/}
echo Underscores = ${#u}
Regards,
Alister
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...
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...
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...
There are two threads started by this user with for the same problem and both threads have several responses. You may be interested to see the comments there: How to find no of underscores in a variable?
Just out of curiosity, why:
count=$(echo -n ${#count})
instead of:
count=${#count}
The echo is not needed, and the -n option to echo (which isn't needed for this example even if you use echo) won't do what you want on most UNIX systems.
Hi Don Cragun...
I live and learn. Thanks for correcting me.
I chose the echo method because I knew it worked and why. I did not know about your shortened version. That is now logged in the _old_ grey matter. ;o)
As for the "-n" I was thinking I was creating a file, my error. I will leave my post as is as you have already pointed it out and shown a better alternative anyhow...
However my variable(s) point still holds true...
Thanks a lot...
Thread closed. Please continue discussion in other thread.