1754321 Members
2878 Online
108813 Solutions
New Discussion юеВ

How to search a word in

 
SOLVED
Go to solution
CA1490051
Frequent Advisor

How to search a word in

Hi ,

Can i know what is the command to search for a word in subsequent directories.

1) I am using
find . -type f -print | grep "hi" *
but it is not working please correct me if i am wrong.
2) Also what should i do if i want to search in a particular files of subsequent directories
ex- if i wnat to search the word "hi" i only the *.cpp files

3) Cant we use xargs in HP- UNIX or is there any alternative for this.

Please suggest some solutions for the above queries.

thanks in advance
Vikram
6 REPLIES 6
Dennis Handly
Acclaimed Contributor
Solution

Re: How to search a word in

grep doesn't take its list of files from stdin. You must use:
$ find . -type f -exec grep "hi" {} +

2) to find .cpp:
$ find . -type f -name "*.cpp" -exec grep "hi" {} +

3) Why would you settle using xargs when you can use -exec {} +?
Yes, you could use xargs in your original example.
Hemmetter
Esteemed Contributor

Re: How to search a word in

Hi Vikram

1)
find . -type f -exec grep "hi" {} \+

2)
give -name "*cpp" as additional parameter to find:

find . -type f -name "*cpp" -exec grep hi {} \+


3) you can use xargs:
find . -type f | xargs grep "hi"

but beware of HP-UX find does not have -print0, thus you may have problems with Path/filenames with spaces.

rgds
HGH




CA1490051
Frequent Advisor

Re: How to search a word in

Hi All,

Thank you very much for the solutions.

I am able to search the word in subsequent directories using the command

find . -type f -exec grep hi \+ | more

but the problem is if i dont give more, it is crashing the terminal i think it is getting into some infinite loop.

Please suggest some solution for this.

thanks & regards
Vikram
Dennis Handly
Acclaimed Contributor

Re: How to search a word in

>but the problem is if i dont give more, it is crashing the terminal i think it is getting into some infinite loop.

You could finding "hi" in binary files. Send the output to a file then vi/more the file.
MurugesanGCT
Advisor

Re: How to search a word in

To know the only the file names(grep -l) and using more on that use:

With -exec in find command
find ./ -type f -name "*.cpp" -exec grep -l hi {} \; | more

With xargs command
find ./ -type f -name "*.edi" | xargs grep -l 111111111 | more
http://www.geocities.com/mukeshgct/
CA1490051
Frequent Advisor

Re: How to search a word in

Thank you very much to all.