Sunday, April 26, 2009

Grep is a Beautiful Tool

Grep is a fantastic Unix command line tool. It is used to print out strings of text that match a particular pattern specified by the user. It is especially useful when combined with other commands and piping the output into the grep tool.

For example, I am working on a project and today I was faced with a log file that was over 5000 lines. However, only about 1500 of those lines contained information that was useful. All the rest of the lines referenced a connection being created. Lucky for me, all of those lines contained the string "Got an IP Packet" or "Got an ICMP Packet." By using grep, I was able to quickly and easily create a second log file containing only the useful information. Here's how I did it:
cat log.txt | grep -v "Got an " > log-trim.txt
The cat command will print out the contents of a file to the terminal, in this case that file is log.txt. That vertical bar is the symbol for pipe, which will send the results of the command to the left of it to the command on its right. In this case, that means it took the results of the cat command and instead of printing it to the console it send the results to grep. Grep will ordinarily read through a chunk of text from beginning to end and print out every line that contains the specified string somewhere in that line. The -v option reverses that behavior so it will print every line except for those containing the specified string. I only want the lines that do not contain the string "Got an " anywhere in them. Finally, the > symbol says to send the results of the grep command to a file instead of displaying the results at the Terminal.

Now I have a log file of just over 1500 lines instead of one with well over 5000 lines, most of which are useless to me.

The title of this post comes from the blog of programmer Eric Wendelin. Here is a link to his post on grep.

No comments:

Post a Comment