LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 12-11-2009, 05:13 PM   #1
kj6loh
Member
 
Registered: Jun 2004
Posts: 47

Rep: Reputation: 17
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.
 
Old 12-11-2009, 05:27 PM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
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.
Old 12-11-2009, 07:04 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
do it like this
Code:
find /path -type f -iname "* *"
 
0 members found this post helpful.
Old 12-12-2009, 08:23 AM   #4
kj6loh
Member
 
Registered: Jun 2004
Posts: 47

Original Poster
Rep: Reputation: 17
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
 
Old 12-12-2009, 08:28 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 12-12-2009, 08:45 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 12-12-2009, 08:54 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by kj6loh View Post
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*"
 
Old 12-12-2009, 09:04 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
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.
 
Old 12-12-2009, 09:09 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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).
 
Old 12-12-2009, 09:10 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 12-12-2009, 09:10 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by catkin View Post
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..
 
Old 12-12-2009, 09:12 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
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
 
Old 12-12-2009, 09:27 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
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
 
Old 12-12-2009, 09:35 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by catkin View Post
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
 
Old 12-12-2009, 10:00 AM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
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
 
  


Reply

Tags
find, whitespace


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find command and file names with whitespace sir_woland Linux - General 13 06-04-2018 03:53 PM
Stripping lines versus stripping bytes in a bash subshell. poorman_installer Programming 9 10-21-2009 08:36 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
[SOLVED] echoing whitespace from a command substitution GahseyFan Linux - General 2 05-16-2009 04:58 AM
Making find do search in a directory that has whitespace in its pathname xode Linux - Software 2 04-18-2006 10:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration