Find a file with text "abc"
I am looking for a find function which
will seek a file containing a text string. Don't say grep because it doesn't work. I'm looking for something I can do on the command line. |
Tell us what you're doing -- grep _should_ do it. Maybe you forgot to include the insensitive search or the recursive option? Please post the command you entered at the command line.
|
grep grep grep grep grep grep
Of course grep works---but it has to be used in conjuction with other things. File is myfile target string is xyz cat myfile | grep xyz prints out each line of the file that contains "xyz" Returns exit status of 0 if the string is found at least once, and 1 if there were no matches. Follow with echo $? to display the exit status. Put all this together and you can write a script that tells you what files contain the string--and the number of occurences, if you desire. To really get into this kind of thing, I suggest "Bash guide for Beginners", by Machtelet Garrels......tldp.org This is inelegant and only one of many ways to do the same thing. |
To add two simple searches for files containing xyz in the current directory and containing folders, both output just the filename, not the line containing the search pattern itself:
grep -R -l xyz * find . -type f -exec grep -l xyz {} \; -- searches also in hidden directories and files |
Works now,
the cat filename | grep xyz and the grep -R -l xyz * I don't understand what went wrong before--stress. find . -type f -exec grep -l xyz {} \; => error:find paths must precede expression a pipe missing? |
Quote:
|
Quote:
|
Quote:
One advantage of the way I wrote it could be readability for the beginner. |
Quote:
I ran a small test on a 200MByte text file. Don't know if it's really a reliable one: Code:
time grep mytxt myfile It surprised me that the 'cat' version actually was faster everytime it was executed. If other processes would have influenced the results, that would be equally true for both versions of the command. PS: 'mytxt' only occured once as the first word on the last line PS2: results are rounded |
All times are GMT -5. The time now is 03:03 PM. |