What is the Best way to search files for a string??


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers What is the Best way to search files for a string??
# 8  
Old 03-24-2008
I think that will work. The live test I did locked me up.....but I think because it found the string in an executable file and tried to display it.

If I only wanted to search C source files (test.c) would I use.....

grep -i <pattern> `find . -name "*.c"`
# 9  
Old 03-24-2008
I anticipated the solution on the first answer but for the completeness, find is mostly used this way:

Code:
find . -name "*" -exec grep -i <pattern> {} \;

Regards

Last edited by Franklin52; 03-24-2008 at 06:29 PM..
# 10  
Old 03-24-2008
Quote:
Originally Posted by 35Soinc
Ok, I'm getting closer. I found a grep command that is somewhat working.......

grep -i 'string' *

This is giving me what I need, but is only searching the present directory.

How do I get this command to also search the sub-directories ??
Keep It Simple :-)

Code:
grep -ir 'string' *

# 11  
Old 03-25-2008
Quote:
Originally Posted by xonicman
Keep It Simple :-)

Code:
grep -ir 'string' *

the -ir gives me an error Smilie

[unix] mark[449]: grep -ir 'monkey' *
grep: illegal option -- r
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
[-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
-f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]
[unix] mark[450]:
# 12  
Old 03-25-2008
Quote:
Originally Posted by Franklin52
I anticipated the solution on the first answer but for the completeness, find is mostly used this way:

Code:
find . -name "*" -exec grep -i <pattern> {} \;

Regards
This code did serach the sub directories and display the line in the file......BUT now does NOT include the file name containing the string Smilie

[unix] mark[24]: pwd
/home
[unix] mark[25]: find . -name "*.c" -exec grep -i 'monkey' {} \;
find: cannot open ./mark/oldetc
this line contains the word monkey.
this line contains the words monkey and turtle.
[unix] mark[26]:
# 13  
Old 03-25-2008
Quote:
Originally Posted by 35Soinc
the -ir gives me an error Smilie

[unix] mark[449]: grep -ir 'monkey' *
grep: illegal option -- r
Ups... sorry. I've not read carefully, that you use HPUX.
# 14  
Old 03-25-2008
Quote:
Originally Posted by 35Soinc
This code did serach the sub directories and display the line in the file......BUT now does NOT include the file name containing the string
Add a /dev/null, or see if your grep has an option to force it to display the file name even if it only greps one file.

Code:
find . -name "*" -exec grep -i <pattern> /dev/null {} \;

Oh, the -name "*" part is probably redundant. Try to leave it off and see if your find chokes.

It's surprising what people will get themselves into just so they don't have to download, build, and install GNU greputils. That would be my recommended solution for your inevitable follow-up problems.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loop through the folders and search for particular string in files

Hello, Opearting System Environment : HP Unix B.11.31 U I look for script to On specific folders list On specific filelist Search for given string For Example : r48_buildlib.txt contains wpr480.0_20161027 wpr480.0_20161114 wpr481.0_20161208 wpr482.0_20161222... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. Shell Programming and Scripting

Read files incrementally and search for particular string.

Example I have following requirements where i need to search for particular string from the log files.Files will be archived with number attached end to it and creates a new log file. First Day i will ran at 8:00 AM Filename:a.log1 Wed Aug 24 04:46:34... (1 Reply)
Discussion started by: nareshnani211
1 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

Help with search string between two files

Hi, Basically i want to search for a string in file two based on the input file one and if it matches get the nextline and print the value of the field name. cat one abc xyz defcat two <src> <name="path/to/abc" test="value_version"> <new name="Y2" > </src> <src> <name="path/to/xyz"... (5 Replies)
Discussion started by: greet_sed
5 Replies

6. Shell Programming and Scripting

Search and replace string in files

I'm trying to remove the following string from several files. <img heigth="1" width="1" border="0" src="http://myteenmovies.net/t.php?id=5540372">I'm using the following script #!/bin/bash # This script will search and replace all regular files for a string # supplied by the user and... (1 Reply)
Discussion started by: d13g0sv
1 Replies

7. UNIX for Dummies Questions & Answers

Search for Files that DONT contain a string

How do I search for files that dont contain a certain string? I am currently trying find ./logs -size +1c -exec grep -l 'Process Complete' {} \; -exec ls -l {} \; > $TOD Which gives me files that are reater han 0 file size and contain the string 'Process complete' but I want files that DONT... (13 Replies)
Discussion started by: tonydsam
13 Replies

8. UNIX for Dummies Questions & Answers

String Search within Text Files

I have many scripts in directories and sub-directories that I would like to search for a specific string. How would I do that? (1 Reply)
Discussion started by: bggibson
1 Replies

9. UNIX for Dummies Questions & Answers

Search files for a string in the remote machine

Hi all, I want to search the files in a remote machine for a particular string. The SSH command I wrote is giving an error even when the syntax is correct. ssh user@hostmachine find . -name "*.txt" -exec grep "ARIVU" '{}' \; The error it gives is find: missing argument to `-exec' When the... (2 Replies)
Discussion started by: a_rivu
2 Replies

10. UNIX for Dummies Questions & Answers

Search all files for specific string

Hi Friends, How can I search all files in all slices on a unix system for a particular string within the file. e.g search string 'oracle' Thanks (4 Replies)
Discussion started by: sureshy
4 Replies
Login or Register to Ask a Question