ee23 find and -regex - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Base System > General

General General questions about the FreeBSD operating system. Ask here if your question does not fit elsewhere.

Reply
 
Thread Tools Display Modes
  #1  
Old September 19th, 2011, 20:07
Business_Woman's Avatar
Business_Woman Business_Woman is offline
Member
 
Join Date: Nov 2008
Posts: 135
Thanks: 1
Thanked 5 Times in 5 Posts
Default find and -regex

Hi,

This question is purely academic.

A simple
Code:
ls /bin | egrep '^..$'
gives a nice list of all the binaries that are two characters long, mv cp ls etc..

But for some reason this doesn't work
Code:
 find -E /bin -regex '^..$'
Why?
__________________
The American lifestyle is not negotiable.

Last edited by DutchDaemon; September 20th, 2011 at 01:00.
Reply With Quote
  #2  
Old September 19th, 2011, 21:15
respite's Avatar
respite respite is offline
Junior Member
 
Join Date: Nov 2009
Location: Los Angeles
Posts: 32
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Not entirely sure as I never use find regex's, though ls will output only the filename while find will produce the full path.
Reply With Quote
  #3  
Old September 19th, 2011, 21:16
kpa kpa is offline
Senior Member
 
Join Date: Jul 2010
Location: People's Technocratic Republic of Finland
Posts: 1,980
Thanks: 44
Thanked 460 Times in 390 Posts
Default

You're matching the whole string with -regex so you have to account for the /bin/ at the beginning.

Code:
 find -E /bin -regex '.*/..$'
Reply With Quote
The Following User Says Thank You to kpa For This Useful Post:
Business_Woman (September 19th, 2011)
  #4  
Old September 19th, 2011, 21:20
Business_Woman's Avatar
Business_Woman Business_Woman is offline
Member
 
Join Date: Nov 2008
Posts: 135
Thanks: 1
Thanked 5 Times in 5 Posts
Default

Thank you.
__________________
The American lifestyle is not negotiable.
Reply With Quote
  #5  
Old September 19th, 2011, 21:24
wblock@'s Avatar
wblock@ wblock@ is offline
Moderator
 
Join Date: Sep 2009
Location: Milky Way galaxy
Posts: 7,701
Thanks: 429
Thanked 1,757 Times in 1,456 Posts
Default

find(1):
Code:
    -regex pattern
             True if the whole path of the file matches pattern using regular
             expression.  To match a file named “./foo/xyzzy”, you can use the
             regular expression “.*/[xyz]*” or “.*/foo/.*”, but not “xyzzy” or
             “/foo/”.
Let's see what the input to that regex really is:
Code:
% find -E /bin -print
/bin
/bin/cat
/bin/chflags
/bin/chmod
/bin/cp
/bin/chio
...
Aha. find returns the whole path. My use of find(1) is mostly by rote; if there's a way to get it to return basenames only, then it would work.
Reply With Quote
  #6  
Old September 19th, 2011, 21:40
Business_Woman's Avatar
Business_Woman Business_Woman is offline
Member
 
Join Date: Nov 2008
Posts: 135
Thanks: 1
Thanked 5 Times in 5 Posts
Default

yes, sorry. I should have read the man page more careful >_>

If i want to match all binaries in /bin that are three characters long and starts with 'p'.

Code:
find -E /bin -regex '\( .*/.. -and -regex '.*\^p \)'
Hm?
__________________
The American lifestyle is not negotiable.
Reply With Quote
  #7  
Old September 20th, 2011, 06:03
wblock@'s Avatar
wblock@ wblock@ is offline
Moderator
 
Join Date: Sep 2009
Location: Milky Way galaxy
Posts: 7,701
Thanks: 429
Thanked 1,757 Times in 1,456 Posts
Default

Easier to do in a single regex:
% find /bin -regex '.*/p..$'

But for this use, ls and grep are more appropriate, shorter and simpler.
% ls /bin | grep '^p..$'
Reply With Quote
Reply

Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Solved] simple regex (not for me) vingtage Userland Programming & Scripting 3 May 20th, 2010 21:38
[Solved] RegEx Not Matching - Why??? Ruler2112 Web & Network Services 6 March 15th, 2010 23:42
variable in grep regex Just_Johnny Userland Programming & Scripting 1 November 1st, 2009 09:47
C++ regex: is there a way to use (.*?) ChrisCphDK Userland Programming & Scripting 7 August 15th, 2009 10:36
[Solved] find and regex clinty Userland Programming & Scripting 4 February 27th, 2009 18:32


All times are GMT +1. The time now is 13:56.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0