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


4 Years
Discussion Span
Last Post by JeoSaurus

The output of grep and other commands can be saved to a file by using a greater-than '>' symbol to redirect the standard output stream. Try something like...

grep "some pattern" some-file-to-search.txt > the-results.txt

To double space the resulting file, you might want to try 'sed', the steam editor. See if this works...

sed G your-file.txt

I like to use 'tee' to print the results to the console AND to a file. Also, the "--color=auto" option to grep can help with readability of output, but I'm not sure how well it works in all shells when reading the results from a text file. Try something like this:

grep --color=auto "pattern" /path/to/file.txt | tee -a logfile.txt

I like the idea of using sed to double-space, but you can accomplish the same thing (grep + sed G) in one line of awk:

awk '/pattern/ {print $0 "\n"}' /path/to/file.txt

I hope this helps!

Votes + Comments
Well done Gromit :-)

...but you can accomplish the same thing (grep + sed G) in one line of awk

True, but piping the output to sed makes it pretty trivial...

grep "some pattern" some-file.txt | sed G

Edited by LaxLoafer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.