bash script to find pattern in text file and return entire line
I need to make a bash script which loops through a bunch of .txt files in a directory, then searches each .txt for a string, and returns the entire line that string appears on
I know how to look through all the .txt files in the directory,
I just need to be pointed in the right direction for searching the file itself, and ret开发者_开发技巧urning a line based on a match in that line
Within one dir
grep "search string" *.txt
Search or go to sub-dir
find /full/path/to/dir -name "*.txt" -exec grep "search string" {} ;\
you can use a loop:
for i in $(find|grep .txt); do grep "search" "$i";
If you also want to print the filename with each match, add -H
to grep
One slight addition. Add -n
to return the line.
grep -rn "foo" *.txt
See grep --help
for more.
精彩评论