LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Stripping whitespace in a find command (https://www.linuxquestions.org/questions/linux-general-1/stripping-whitespace-in-a-find-command-775048/)

kj6loh 12-11-2009 05:13 PM

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.

Komakino 12-11-2009 05:27 PM

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 "\ *")

ghostdog74 12-11-2009 07:04 PM

do it like this
Code:

find /path -type f -iname "* *"

kj6loh 12-12-2009 08:23 AM

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


ghostdog74 12-12-2009 08:28 AM

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.

colucix 12-12-2009 08:45 AM

Why not simply a good-ol' pipe?
Code:

find . -type f | grep -E '.*[bB][ ]+[cC].*'

catkin 12-12-2009 08:54 AM

Quote:

Originally Posted by kj6loh (Post 3788798)
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*"

ghostdog74 12-12-2009 09:04 AM

Quote:

Originally Posted by colucix (Post 3788820)
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.*"

colucix 12-12-2009 09:09 AM

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).

colucix 12-12-2009 09:10 AM

Quote:

Originally Posted by ghostdog74 (Post 3788833)
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.

ghostdog74 12-12-2009 09:10 AM

Quote:

Originally Posted by catkin (Post 3788825)
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..

ghostdog74 12-12-2009 09:12 AM

Quote:

Originally Posted by colucix (Post 3788836)
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

catkin 12-12-2009 09:27 AM

Quote:

Originally Posted by ghostdog74 (Post 3788839)
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


ghostdog74 12-12-2009 09:35 AM

Quote:

Originally Posted by catkin (Post 3788850)
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

catkin 12-12-2009 10:00 AM

Quote:

Originally Posted by ghostdog74 (Post 3788860)
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 03:49 AM.