Output grep results to text file, need cleaner output

后端 未结 2 1648
野性不改
野性不改 2021-01-30 02:25

When using the Grep command to find a search string in a set of files, how do I dump the results to a text file?

Also is there a switch for the Grep command that provide

相关标签:
2条回答
  • 2021-01-30 02:46
    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

    0 讨论(0)
  • 2021-01-30 02:47

    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.

    0 讨论(0)
提交回复
热议问题