Output grep results to text file, need cleaner output [closed]

北城余情 提交于 2019-12-03 02:42:18

问题


When using the Grep command to find a search string in a set of files, is there a way to dump the results to a text file?

Also is there a switch for the Grep command that provides cleaner results for better readability, such as a line feed between each entry?

Perhaps there is a grep script that outputs cleaner results.

Thanks


回答1:


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 on this post




回答2:


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.



来源:https://stackoverflow.com/questions/16631423/output-grep-results-to-text-file-need-cleaner-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!