Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

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

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

    
Is "co_oneruo" 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

3 Answers 3

find .  -regex co_on*eruo* | xargs grep '0LF2C Comdty(.*)20111022(.*)'
share|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

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

share|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 'coptioneruo*' , 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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.