Output grep results to text file, need cleaner output

TextGrepOutput

Text Problem Overview


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 provides cleaner results for better readability, such as a line feed between each entry or a way to justify file names and search results?

For instance, a away to change...

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

to

./file/path: first result

./another/file/path: second result

./a/third/file/path/here: third result

Text Solutions


Solution 1 - Text

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

Solution 2 - Text

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.

Solution 3 - Text

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2398188View Question on Stackoverflow
Solution 1 - TextNir AlfasiView Answer on Stackoverflow
Solution 2 - TextIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 3 - TextdanView Answer on Stackoverflow