I want to search for file names with a regular expression, and grep it using regex. I tried this:

find .  -name co_on*eruo* | xargs grep '0LF2C Comdty(.*)20111022(.*)'

But it doesn't work. What am I doing incorrectly?

link|improve this question
Is "co_on*eruo*" a regex or a glob? Either way, I'm sure it should be quoted. I can't tell you how to fix it since you don't say what is wrong. – Johnsyweb Oct 26 '11 at 9:25
feedback

migrated from stackoverflow.com Oct 26 '11 at 9:23

This question came from our site for professional and enthusiast programmers.

3 Answers

find .  -regex co_on*eruo* | xargs grep '0LF2C Comdty(.*)20111022(.*)'
link|improve this answer
You still need to quote co_on*eruo* so the shell doesn't expand it. – Keith Thompson Oct 26 '11 at 2:53
Welcome to Super User! Could you provide some explanation about why your code works? – Tom Wijsman Oct 31 '11 at 21:19
feedback

First of all, exactly how doesn't it work?

You need to quote the pattern (note: it's not a regex) to keep the shell from expanding it:

find .  -name 'co_on*eruo*' | xargs grep '0LF2C Comdty(.*)20111022(.*)'

EDIT

But if that's really supposed to be a regex (i.e., you want to match "co_o", then zero or more "n"s, then "eru", then zero or more "o"s), then as Tim says you need to use -regex rather than -name. (That's probably a GNU-specific feature; if find --version doesn't say something like find (GNU findutils) 4.4.2, then your find might not support -regex.)

link|improve this answer
it can't find any file in the first part . – user595234 Oct 26 '11 at 2:53
my find version: find --version GNU find version 4.2.27 Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX – user595234 Oct 26 '11 at 2:56
I changed to find . -regex 'c*option*eruo*' , still no result. – user595234 Oct 26 '11 at 2:59
That's not the same regular expression as the one in your question. Without knowing what files you have and what they contain, we can't tell what's going wrong. – Keith Thompson Oct 26 '11 at 3:17
feedback

Did you wan't the parantheses in the grep expression, or where they only there since you thought you needed them for the regexp functionality?

As it looks, you could probably do what you want with grep directly:

grep -R --include='co_on*eruo*' '0LF2C Comdty.*20111022.*'

Perhaps you want -name to span directory names as well - it doesn't. Give more info on the exact paths if you want a solution for that.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown