将 grep 结果输出到文本文件,需要更清晰的输出

当使用 Grep 命令在一组文件中查找搜索字符串时,如何将结果转储到文本文件中?

还有一个开关的 Grep 命令,提供更清晰的结果,更好的可读性,如每个条目之间的行饲料或调整文件名和搜索结果的方法?

例如,一个改变..。

./file/path: first result
./another/file/path: second result
./a/third/file/path/here: third result

./file/path: first result


./another/file/path: second result


./a/third/file/path/here: third result
420788 次浏览

Redirection of program output is performed by the shell.

grep ... > output.txt

grep has no mechanism for adding blank lines between each match, but does provide options such as context around the matched line and colorization of the match itself. See the grep(1) man page for details, specifically the -C and --color options.

grep -n "YOUR SEARCH STRING" * > output-file

The -n will print the line number and the > will redirect grep-results to the output-file.
If you want to "clean" the results you can filter them using pipe | for example:
grep -n "test" * | grep -v "mytest" > output-file will match all the lines that have the string "test" except the lines that match the string "mytest" (that's the switch -v) - and will redirect the result to an output file.
A few good grep-tips can be found in this post

To add a blank line between lines of text in grep output to make it easier to read, pipe (|) it through sed:

grep text-to-search-for file-to-grep | sed G