Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
12-11-2009, 05:13 PM
#1
Member
Registered: Jun 2004
Posts: 47
Rep:
Stripping whitespace in a find command
I don't want to strip the beginning or the end.
but I want to do something like
find <dir> -iname *<1wd><spaces><2wd>*
I've tried
find <dir> -iname *1wd\ +2wd*
find <dir> -iname *1wd[\ ]+2wd*
find <dir> -iname *1wd[\ ]\+2wd*
to no avail.
12-11-2009, 05:27 PM
#2
Senior Member
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938
Rep:
You want something along the lines of:
Code:
find <dir> -iregex "*wd\ *2wd"
but the -iregex syntax is odd because it matches on the whole file name, even if you've already specified a directory.
e.g. the file:
/folder/afile.bat
will match with
-iregex "folder/a.*\.bat"
but not
-regex "a.*\.bat"
(and any number of spaces would be "\ *")
1 members found this post helpful.
12-11-2009, 07:04 PM
#3
Senior Member
Registered: Aug 2006
Posts: 2,697
do it like this
Code:
find /path -type f -iname "* *"
0 members found this post helpful.
12-12-2009, 08:23 AM
#4
Member
Registered: Jun 2004
Posts: 47
Original Poster
Rep:
ghostdog yours would not work and I tell you why that gets every file with a space.
Let me give you guys a more concrete example.
I have 11 files
Code:
aaaaab caaaa
aaaad daaaaaa
aaab caaaa
aae faa
aaaaaaab c
aab ccccaaa
ddddd
b c
aaaaC B
B Caaaa
baaac
I want to get all the files with b then c regardless of the number of spaces in between and regardless of case. So this is the output:
Code:
aaaaab caaaa
aaab caaaa
aaaaaaab c
aab ccccaaa
b c
B Caaaa
Last edited by kj6loh; 12-12-2009 at 08:26 AM .
Reason: formatting
12-12-2009, 08:28 AM
#5
Senior Member
Registered: Aug 2006
Posts: 2,697
then add your "b"s and "c"s into the expression. Its easy, so I leave it to you to experiment. the command line is there for you to use.
12-12-2009, 08:45 AM
#6
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
Why not simply a good-ol' pipe?
Code:
find . -type f | grep -E '.*[bB][ ]+[cC].*'
Last edited by colucix; 12-12-2009 at 08:49 AM .
Reason: Modified regexp for case insensitive requirement
12-12-2009, 08:54 AM
#7
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Quote:
Originally Posted by
kj6loh
I want to get all the files with b then c regardless of the number of spaces in between and regardless of case.
Then you want
Code:
find /path -type f -iname "wrd1* *wrd2"
or maybe
Code:
find /path -type f -iname "*wrd1* *wrd2*"
12-12-2009, 09:04 AM
#8
Senior Member
Registered: Aug 2006
Posts: 2,697
Quote:
Originally Posted by
colucix
Why not simply a good-ol' pipe?
Code:
find . -type f | grep -E '.*[bB][ ]+[cC].*'
because you are not going to call grep for every file it finds, even the ones that are not needed?? this produces overhead. Its better to let find do the walk and talk.
Code:
find . -type f <for OP to find out> ".*b[ ]+c.*"
Last edited by ghostdog74; 12-12-2009 at 09:09 AM .
12-12-2009, 09:09 AM
#9
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
Putting together suggestions by Komakino, ghostdog74 and catkin, most likely this is what you're looking for:
Code:
find . -type f -iname "*b* *c*" -iregex '.*b[ ]+c.*'
the output will not contain matching
directories (see note by Komakino).
12-12-2009, 09:10 AM
#10
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
Quote:
Originally Posted by
ghostdog74
because you are not going to call grep for every file it finds, even the ones that are not needed?? this produces overhead.
That's true. I stand corrected.
12-12-2009, 09:10 AM
#11
Senior Member
Registered: Aug 2006
Posts: 2,697
Quote:
Originally Posted by
catkin
Then you want
Code:
find /path -type f -iname "wrd1* *wrd2"
or maybe
Code:
find /path -type f -iname "*wrd1* *wrd2*"
according to OP, that's not his requirement. he wants to find multiple spaces as well..
12-12-2009, 09:12 AM
#12
Senior Member
Registered: Aug 2006
Posts: 2,697
Quote:
Originally Posted by
colucix
Putting together suggestions by Komakino, ghostdog74 and catkin, most likely this is what you're looking for:
Code:
find . -type f -iname "*b* *c*" -iregex '.*b[ ]+c.*'
the output will not contain matching
directories (see note by Komakino).
don't need -iname
12-12-2009, 09:27 AM
#13
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Quote:
Originally Posted by
ghostdog74
according to OP, that's not his requirement. he wants to find multiple spaces as well..
The * pattern elements also match spaces.
Code:
c:/tmp$ touch 'a b'
c:/tmp$ find . -iname 'a* *b'
./a b
12-12-2009, 09:35 AM
#14
Senior Member
Registered: Aug 2006
Posts: 2,697
Quote:
Originally Posted by
catkin
The * pattern elements also match spaces.
Code:
c:/tmp$ touch 'a b'
c:/tmp$ find . -iname 'a* *b'
./a b
i originally wanted to give him this
Code:
find . -iname "*b* *c*"
but i am considering the fact that he might have files like
Code:
blahb blah cblahblah
so, anyway, best to let OP decide
12-12-2009, 10:00 AM
#15
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Quote:
Originally Posted by
ghostdog74
i originally wanted to give him this
Code:
find . -iname "*b* *c*"
but i am considering the fact that he might have files like
Code:
blahb blah cblahblah
so, anyway, best to let OP decide
Sorry -- you are right; I misunderstood the requirement which is clearly expressed by example in
this post
All times are GMT -5. The time now is 10:41 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News