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


Recommended Answers

All 4 Replies

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!

commented: Well done Gromit :-) +3

...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

Right! There's nothing wrong with it, I just have a personal bias against pipes ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.