LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to search word in group of files using command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-search-word-in-group-of-files-using-command-760953/)

sumeet inani 10-10-2009 05:15 AM

how to search word in group of files using command
 
hi
i am using openSUSE 10.3 & like to use power of terminal to do tasks.I want to search all files containing word 'echo' in given folder.What should be command.

Ahmed 10-10-2009 05:22 AM

Try the "grep" command. Go into the directory you mean via command line and do (//EDIT: example improved to avoid confusion):

Code:

$ grep "yoursearchpattern" *
It will go through all files inside the folder (therefore the '*') and tell you which ones contain the search pattern. For more options execute 'man grep' to read the instructions..

The grep command is very useful by the way if you want to view the output of a file and filter this output for a given word. Say you have a file called blah.conf and want to view its contents:

Code:

$ cat blah.conf
To find out whether or not there's a line containing the word 'example' without having to read through the entire file, you pipe it's output to the grep command using the symbol |, which returns only the lines with that word, as follows:

Code:

$ cat blah.conf |grep example
Try it out with a file and search term of your choice. Just thought I'd give you an example of the power of the command line :)

-A

mariogarcia 10-10-2009 05:26 AM

you could do

find . | xargs grep -i pattern

it will show all the files in the current folder that have the pattern whether it is in lowercase or uppercase.

kind regards

lutusp 10-10-2009 05:46 PM

Quote:

Originally Posted by Ahmed (Post 3714424)
Try the "grep" command. Go into the directory you mean via command line and do:

Code:

$ grep (yoursearchpattern) *
[/CODE]

Not a very good example. The parentheses need to be quoted to keep Bash from mistaking your intentions:


Code:

$ grep (yoursearchpattern) *
bash: syntax error near unexpected token `yoursearchpattern'


jmc1987 10-10-2009 06:14 PM

Quote:

Originally Posted by lutusp (Post 3714888)
Not a very good example. The parentheses need to be quoted to keep Bash from mistaking your intentions:


Code:

$ grep (yoursearchpattern) *
bash: syntax error near unexpected token `yoursearchpattern'


Wouldn't it be like

Code:

$ grep /(yoursearchpattern/) *

Doculus 10-10-2009 06:30 PM

Quote:

Originally Posted by mariogarcia (Post 3714425)
find . | xargs grep -i pattern

Like this you will have a process started for each of the file, plus 2 for find and xargs. Not very environment friendly. Also you loose information in which file the pattern was found, which can be interesting also, or maybe the only interest?

Code:

grep -i pattern -r .
Results in 1 process start, and you will get the filenames also. If you are not interested in the whole result lines, only filenames, you can even write:

Code:

grep -i pattern -rc .
See manpage: grep(1)

lutusp 10-10-2009 06:36 PM

Quote:

Originally Posted by jmc1987 (Post 3714894)
Wouldn't it be like

Code:

$ grep /(yoursearchpattern/) *

No, you would use this:

Code:

$ grep \(yoursearchpattern\) *
But this is more reliable and less confusing:

Code:

$ grep "(yoursearchpattern)" *

fusion1275 10-10-2009 07:12 PM

Quote:

grep -ir <pattern_name> *
job done!

Ahmed 10-10-2009 07:17 PM

Quote:

Originally Posted by lutusp (Post 3714888)
Not a very good example. The parentheses need to be quoted to keep Bash from mistaking your intentions:


Code:

$ grep (yoursearchpattern) *
bash: syntax error near unexpected token `yoursearchpattern'


I didn't mean the parenthesis to be part of the search pattern, in lots of cases they don't. It was just a way of highlighting where the search pattern should go. Anyway to make things clearer, I edited my first post. Thanks for the tip.

-A

smeezekitty 10-10-2009 07:18 PM

Code:

ls | grep "pattern"
is that it?

sycamorex 10-10-2009 07:34 PM

Quote:

Originally Posted by smeezekitty (Post 3714926)
Code:

ls | grep "pattern"
is that it?

It will only search file names, not their contents.

lutusp 10-10-2009 08:07 PM

Quote:

Originally Posted by Ahmed (Post 3714925)
I didn't mean the parenthesis to be part of the search pattern, in lots of cases they don't. It was just a way of highlighting where the search pattern should go. Anyway to make things clearer, I edited my first post. Thanks for the tip.

-A

I assumed you included the parentheses for a reason -- to suggest a regular expression. Parentheses are often used in grep searches, like this example:

Code:

find -type f | grep -iP "\.(gif|jpe?g|png)$" | while read path
do
  ...
done

This example selects files that end in common graphic file suffixes. When I see parentheses in grep examples, I assume they are meant to represent a regular-expression multiple-choice selector like this example.

In any case, the parentheses need to be managed to keep them from being interpreted by the shell -- that was my earlier point.

cola 10-10-2009 09:52 PM

Quote:

Originally Posted by sumeet inani (Post 3714418)
hi
i am using openSUSE 10.3 & like to use power of terminal to do tasks.I want to search all files containing word 'echo' in given folder.What should be command.

Code:

ls -l | grep <pattern>

sumeet inani 10-12-2009 12:15 AM

WHAT I HAVE LEARNT
I noticed that if I write
grep pattern *
then it gives file with corresponding lines.Also I should enclose search parameter in single or double quotes if it contains space.

pixellany 10-12-2009 12:30 AM

You should also use quotes when the search pattern has any wildcards---and in a few other situations.

You should also read up on the difference between single and double quotes---I would start with the Bash Guide for Beginners---free at http://tldp.org


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