LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   about grep (https://www.linuxquestions.org/questions/linux-newbie-8/about-grep-747197/)

elainelaw 08-13-2009 01:09 AM

about grep
 
I would like to grep the word "ABC" from a directory , if I use "grep ABC *" , it will output all word that contains ABC ( eg. ABCD , ABCDE.... ) , can advise if I only want to find the file have the word ABC , what can I do ? thx

jrtayloriv 08-13-2009 01:31 AM

It's difficult to understand what you are asking for.

If you are looking to find all files in the current directory, or subdirectories thereof, which contain the pattern ABC anywhere in their name, then you want:

Code:

find . -name '*ABC*'
If, on the other hand, you want files that begin with the pattern ABC, then you do:

Code:

find . -name 'ABC*'
See man find for more info.

--jrtayloriv

tredegar 08-13-2009 01:32 AM

grep " ABC " *
?

jlliagre 08-13-2009 01:46 AM

Code:

ls *ABC*
?

gnashley 08-13-2009 01:54 AM

\b for word boundary:
grep '\bABC\b' *

jrtayloriv 08-13-2009 01:59 AM

Quote:

Originally Posted by jlliagre (Post 3641468)
Code:

ls *ABC*
?

I don't think that's what the OP is looking for -- that would list all of the files in any directory that contains the pattern ABC in its (i.e. the directory's) name.

--jrtayloriv

visitnag 08-13-2009 01:59 AM

if your ABC is having spaces around then try this...

grep " ABC " input > output

or

grep "ABC " input > output

or

grep -E " ABC |ABC " input > output

elainelaw 08-13-2009 02:06 AM

it seems not work,

my requirement is not difficult

in my directory , there are some files contains ABC , ABCD , ABCDE , what I want is to use grep to find the file that contains ABC , I use grep "ABC" * , it will show all the files ( includes ABCD , ABCDE ) , can advise what can i do ? thx

jlliagre 08-13-2009 02:32 AM

Quote:

Originally Posted by jrtayloriv (Post 3641475)
I don't think that's what the OP is looking for -- that would list all of the files in any directory that contains the pattern ABC in its (i.e. the directory's) name.

Indeed. You are right.
Quote:

Originally Posted by elainelaw
in my directory , there are some files contains ABC , ABCD , ABCDE , what I want is to use grep to find the file that contains ABC , I use grep "ABC" * , it will show all the files ( includes ABCD , ABCDE ) , can advise what can i do ? thx

ABCD and ABCD contains ABC. You should clarify your requirements.

Perhaps what you are looking for is:
Code:

ls | grep -w ABC

geekfreaking 08-13-2009 04:25 AM

Another way is:
Code:

find . -name ABC
which will find all files in the current directory and subdirectories with the name ABC and only the name ABC.

nowonmai 08-13-2009 04:47 AM

Are you looking for files that contain ABC or filenames that contain ABC?


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