How can I use find command to search string/pattern in a file recursively?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I use find command to search string/pattern in a file recursively?
# 8  
Old 10-03-2016
Don, I think you mean
Code:
#!/bin/your_shell
cat "$@" | grep -F 'make'

grep reads from the pipe stream, and cannot see the file names.
It is equivalent to
Code:
grep -F -h 'make' "$@"

where grep sees the file names and the -h prevents printing them as a prefix.
Unfortunately grep by default prefixes the file names when it has got two or more file names. But not with one file name.
And there is no option to enforce the prefix. Therefore the /dev/null trick: it is one more file name. Nothing is ever found there, but it enforces the prefix. For example
Code:
grep -F 'make' /dev/null "$@"

will prefix the file name even if the "$@" has only one argument (file name).

Last edited by MadeInGermany; 10-03-2016 at 11:44 AM..
# 9  
Old 10-03-2016
Quote:
Originally Posted by Don Cragun
The difference between \; and + terminating a find -exec primary is described above.
Are {} \+ and {} + same?
# 10  
Old 10-03-2016
Quote:
Originally Posted by MadeInGermany
Don, I think you mean
Code:
#!/bin/your_shell
cat "$@" | grep -F 'make'

grep reads from the pipe stream, and cannot see the file names.
It is equivalent to
Code:
grep -F -h 'make' "$@"

where grep sees the file names and the -h prevents printing them as a prefix.
Unfortunately grep by default prefixes the file names when it has got two or more file names. But not with one file name.
And there is no option to enforce the prefix. Therefore the /dev/null trick: it is one more file name. Nothing is ever found there, but it enforces the prefix. For example
Code:
grep -F 'make' /dev/null "$@"

will prefix the file name even if the "$@" has only one argument (file name).
Hi MadeInGermany,
Yes. I agree with all of the above. Note that the code the user posted was:
Code:
-exec cat {} | grep "make"

and I was demonstrating how to get the same output. And, I later suggested using the /dev/null trick because I assume (without any indication from the original poster of this thread) that just having a list of matching lines without knowing what files they came from would not be very useful. I should have made clear in post #7 that the discussion I included in post #3 still applied.
Quote:
Originally Posted by cola
Are {} \+ and {} + same?
Yes. But {} ; cannot be used instead of {} \;. The semicolon has special meaning to the shell and must be escaped to make the shell pass the semicolon to find. The plus-sign is not special to the shell in this context, so it does not need to be escaped.
# 11  
Old 10-04-2016
Code:
find . -type f -exec grep "make" {} \;

Is it possible to get the same output with find without using -exec grep? Does find have anything to search string in files?
# 12  
Old 10-04-2016
I don't know of any version of find that has a primary that searches file contents. And you haven't bothered to tell us what operating system you're using, so we can't check the find man page on your system to see if it has any extensions that might provide that capability. Why don't you read the find man page on your system and see if you can find any primaries or options that would do what you want with the find that is available on your system?
# 13  
Old 10-04-2016
In addition to what DonCragun said, you could also use

Code:
grep -rF make .

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

Experts, Need your support for this awk script. we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
Discussion started by: as7951
6 Replies

2. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

3. UNIX for Advanced & Expert Users

Recursively search the string from a column in no. of files

i have a file named keyword.csv(contains around 8k records) which contains a no. of columns. The 5th column contains all the keywords. I want to recursively search these keywords in all .pl files(around 1k) and display the filename....Afterthat i will use the filename and some of the column from... (3 Replies)
Discussion started by: millan
3 Replies

4. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

5. Shell Programming and Scripting

how to find a pattern from an external file in a directory containing multiple file recursively

Hi, Need your help in this. I have an input file that has multiple enrollment_number, somewhat like 1234567 8901234 9856321 6732187 7623465 Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a... (10 Replies)
Discussion started by: mukulverma2408
10 Replies

6. Shell Programming and Scripting

Awk search for string pattern in delimited file

I've got a semicolon delimited file. I would like to search for fields that match a pattern, and not hardcoded eg "mth". *th=something If the delimited field fulfills this condition, eg. mth=value I would like to print out both key and value for some number comparison. eg. if value > "12"... (5 Replies)
Discussion started by: alienated
5 Replies

7. Shell Programming and Scripting

Using find command to search a pattern in group of files

Hi i am having a group of *.csh files under parent directory. Now i want to search a particular pattern in these group of *.csh files(suppose i need to search a pattern ABC - proj ). Can anyone please tell me how to do it using find command. Thanks in advance sarbjit (4 Replies)
Discussion started by: sarbjit
4 Replies

8. Shell Programming and Scripting

find and replace a search string recursively in files

Hi , I have a directory structure as dir and subdirectories and files under it and so on.now I need to find the files which contain the search string under every dir and subdir and replace . my search string is like searchstring=/a/b string to be replaced=/a/c/b please help. ... (7 Replies)
Discussion started by: mohanpadamata
7 Replies

9. UNIX for Dummies Questions & Answers

String search - Command to find second occurance

Hi, I am new to Unix world. Is there any command which can directly return the second occurance of a particular string in a file? Basically, I want to read the contents of the file from the second occurance of a particualr string. Can be implemented using a loop, but am just wondering if there... (5 Replies)
Discussion started by: saurabhsinha23
5 Replies

10. Shell Programming and Scripting

Search Mulitiple String pattern in a file

Hi, I need to search for a multiple string pattern(5 key words) in a file(similar to a flat file) ,and i need to store the output in a another file . In that file we may have mutiple occurrences of the key words.and i need only the unique. kindly help out. Thanks, Mohana Krishnan (2 Replies)
Discussion started by: krishnan_6015@y
2 Replies
Login or Register to Ask a Question