cancel
Showing results for 
Search instead for 
Did you mean: 

grep command help

rhansen
Frequent Advisor

grep command help

Hello,

I need to grep out a single word from a bunch of files under a directory. I also need to have the filename with it.
Under /usr/tmp I have about 100 servers with the hostname. I need to grep out specific words from each server and the output needs to give the server name and the word. and if possible i need to have the word displayed only once for each server as the word may be present multiple times.

Thanks in advance.
7 REPLIES
TTr
Honored Contributor

Re: grep command help

The "grep -l singleword" will give you the filename if it finds the singleword in it. If the directory is flat you can use "grep -l singleword /pathto/directory/*" otherwise you have to descent to the subdirectories with "find".

For running this process against 100 servers you have to run remsh to each server assuming you can do that without supplying a password.
OldSchool
Honored Contributor

Re: grep command help

"need to grep out a single word from a bunch of files...."
"/usr/tmp I have about 100 servers...."
"need to grep out specific words from each server...."
"server name and the word."

I can't tell if this will meet your requirement or not:

put your list of words in a file (let's call $HOME/pattern

cd /usr/tmp
grep -f $HOME/pattern server* | sort -u

you might have to futz with the "-w", "-i" or other flags...depending on whats actually in the file and what you want to see
Steven Schweda
Honored Contributor

Re: grep command help

Non-psychics may need a clearer explanation
of what you have and what you wish to do with
it. Particularly unclear:

> Under /usr/tmp I have about 100 servers
> with the hostname.

What does it mean to have a "server" under a
directory?

> [... everything else ...]

Perhaps some examples would be more helpful
than these vague and cryptic
non-descriptions.
Kapil Jha
Honored Contributor

Re: grep command help

I need to grep out a single word from a bunch of files under a directory. I also need to have the filename with it.
>>> simple
# grep word * [inside the directory]

Under /usr/tmp I have about 100 servers ....... may be present multiple times.

Could not understand much perhaps you can grep out other stuffs from the above output.
Better you can use awk.

BR,
Kapil+
I am in this small bowl, I wane see the real world......
Raj D.
Honored Contributor

Re: grep command help

rhansen,

>>Under /usr/tmp I have about 100 servers with the hostname. I need to grep out specific words...

- server is a physical thing. You cant keep it under /usr/tmp.

You can grep , may be you can use some regular expression, and sort it , use uniqe ,and pase it out.

If you could , put some pattern of your file here, and take some time to describe the pattern you want in the output. That will make more sense to provide some good reply.




Hth,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Dennis Handly
Acclaimed Contributor

Re: grep command help

>need to grep out a single word from a bunch of files under a directory. I also need to have the filename with it.

Adding to what TTr said, you may want to use -w for a delimited match:
grep -l -w singleword

>Under /usr/tmp I have about 100 servers

Is this a different question?
Is this really: on 100 servers, I have /usr/tmp and ...
rhansen
Frequent Advisor

Re: grep command help

None.