Operating System - HP-UX
1834903 Members
3086 Online
110071 Solutions
New Discussion

Re: How do I search for a particular string inside of a File?

 
SOLVED
Go to solution
Shaun Aldrich
Frequent Advisor

How do I search for a particular string inside of a File?

Hello Everyone,

Just a quick and probably very easy question I am trying to find out. Does anyone know how do I search for a particular string inside of a file?

This is different than from the command line where I usually use the Find command.

Thanks in advance for your help....

S Aldrich
12 REPLIES 12
Alan Riggs
Honored Contributor

Re: How do I search for a particular string inside of a File?

The command you want is grep.

grep "string" file(s)
Rick Garland
Honored Contributor

Re: How do I search for a particular string inside of a File?

The command 'grep' is what you would use. There are options to grep so as to be case insensitive or to match exactly or to ...

Shaun Aldrich
Frequent Advisor

Re: How do I search for a particular string inside of a File?

Thanks for the responses... I will need to rephrase the question to How can I search for a particular string inside of a File scanning multiple directories & paths? If you use grep that is just for the local directory.

Thanks for your help....
Kofi ARTHIABAH
Honored Contributor

Re: How do I search for a particular string inside of a File?

Use a combination of find and grep:

grep $STRING `find . -print` `find /other/paths/ -name "pattern" -print`

ie. search from current directory downwards as well as /other/paths/
nothing wrong with me that a few lines of code cannot fix!
Timothy Czarnik
Esteemed Contributor

Re: How do I search for a particular string inside of a File?

Shaun,

Perhaps this may help:

for file in $(ls //*)
do
grep -l "string" $file
done

From the command line, this will create a listing of all files under the directory. The -l (ell) in grep tells it to print the filename if the "string" is found.

Ugly, but it'll work.

-Tim
Hey! Who turned out the lights!
James R. Ferguson
Acclaimed Contributor

Re: How do I search for a particular string inside of a File?

Shaun:

Imagine you want to find all files in /tmp whose name start with "f", and you want to search them for the string "hello" but don't care about the case of the letters. Then, do:

# find /tmp -name f\* -print -exec grep -i hello {} \;

...JRF...
CHRIS_ANORUO
Honored Contributor

Re: How do I search for a particular string inside of a File?

Use this command "cat filename |grep string"
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Dan Hetzel
Honored Contributor

Re: How do I search for a particular string inside of a File?

In case you search in a VERY large number
of files, and to avoid the "argument list
too long message", use a construct like:

find / -name "*.txt" | xargs grep mypattern

Hope this helps,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Dan Hetzel
Honored Contributor

Re: How do I search for a particular string inside of a File?

Hey, Cool !!

I've been awarded my first 9 points !!

Thanks
Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Kevin Ernst
Regular Advisor

Re: How do I search for a particular string inside of a File?

A command line like

find . -type f -exec grep "string" {} ;

works well, except it's hard to tell exactly *which* files matched the string. (Grep won't echo the filename if it only received one file in its argument list, which is the case when used with a 'find -exec' command.) You can fix this by piping through 'xargs,' which will pass as many arguments as it can to 'grep' on a single command line (within the compiled-in restrictions on environment size). Grep then echos each matched line preceded by ": ".

Or use GNU grep (download the depot at http://hpux.cae.wisc.edu , or a local mirror), which has a -H option to force printing of the filename:

find . -type f -exec ggrep -H "string" {} ;

(And the incredibly useful '-C' option for printing the matched line along with several lines of 'context.')
James R. Ferguson
Acclaimed Contributor
Solution

Re: How do I search for a particular string inside of a File?

Shaun:

With regard to the last post by Kevin, add the '-print' option to see the file name. Thus:

# find . -print -type f -exec grep "string" {} \;

will show the filename along with the "string".

...JRF...
Kevin Ernst
Regular Advisor

Re: How do I search for a particular string inside of a File?

Well, I just learned something. =)