Hello, I use this command to search for text inside files in linux
find ./ -type f -exec grep -l "Text To Find" {} \;
The command works fine, but I would like to automatically print a line containing the text, or, if possible, two lines above the text and two lines after the text.
Other suggestions for finding text and printing strings instead of using find are also welcome .
Thank you very much in advance.
find ./ -type f -exec grep -Hn "Text To Find" {} \;
Use the -A and -B flags to print lines before and after the match:
find ./ -type f -exec grep -Hn -A1 -B1 "Text To Find" {} \;
You can also use grep:
grep -R -Hn -A1 -B1 "Text To Find" *
find:
find
find . -type f -print0 | xargs -0 grep -Hn -C2 "Text To Find"
grep ( -exec ... {}), grep .
grep
-exec ... {}
, -print0, -0 -C2 ( GNU find, xargs grep, Linux, BSD .. Cygwin MinGW, , "" Solaris, HPUX ..)
-print0
-0
-C2
xargs
grep?
grep -r -C2 "Text To Find" *
( , ):
find . -type f -exec grep "text" {} /dev/null \;
, , add -A2 "grep" , -B2 , -C2 .
-A2
-B2
find ./ -type f -exec egrep -H -B 2 -A 2 "Text" '{}' ';'