Originally Published: Saturday, 2 September 2000 Author: Alex Pearsall
Published to: learn_articles_firststep/General Page: 1/1 - [Printable]

You've Got a Friend in Grep!

Ever come across a command that spurts out a long line of cool-looking text, which immediately scrolls past your terminal's viewing limit? "If only there were a way to search through the output for certain information", you're thinking to yourself. Well, there is, and it's called "grep".

   Page 1 of 1  

Ever been typing at the console, experimenting with commands? You try a command here, a command there, and jot down what you think each one does. Then, you come across one command that spurts out a long line of cool looking text, which scrolls past your terminal's viewing limit. You scroll up and down, perhaps looking for a particular string of text to tell you that your video card or network is set up properly. Wish there was an easier way? Well, there is - GREP!

Grep stands for (G)lobal (R)egular (E)xpression (P)arser. This information isn't really necessary, but it's a cool little fact to tell to your friends. You'll wow them with your brain power! Don't waste any mental energy trying to figure out exactly what this means, as you will probably turn red in the face and fall over. To simplify the definition, grep basically displays lines of text in a file that match the search pattern you specify. Mental gears still slipping? This analogy might help:

Pretend you have a screen on a window. You want to allow only ONE type of bug into your house, but leave all the rest outside. This screen allows you to customize the openings so that only the bug of YOUR choice can enter your house. Grep is like this screen. It will search the output of a command (or the contents of a file), and only show you the lines of text in that command or file that match your customized search string.

Now that we have that out of the way, lets move onto the actual raw usage of the command. The most popular usage of grep is with other commands. If you're a bit unsure as to how coomands are strung together, check out the recent FirstStep article on the subject. Back to business, though. The basic syntax is:

<command> | grep <pattern>

A simple example of this would be if I was looking in my dmesg output for "eth0". Dmesg is a utility that will show you the most recent messages from the computer's "boot" (startup) process. It is often necessary to use this command when checking to see if a device (such as an ethernet card, represented by "eth0" in this case) has been properly set up. The usage for a situation like this would be

dmesg | grep eth0 eth0: 3Com 3c905B Cyclone 100baseTx at 0xec00, 00:c0:4f:60:2f:2d, IRQ 11 bridge-eth0: up bridge-eth0: attached

This would scan the output of the command dmesg for any lines with the letters "eth0" in sequence, and display them out on the screen for you. In this case, you can see that a 3Com 3c905B card has been properly detected. If you are looking for more than one word using grep, you will need to surround your pattern in quotes. For example, if I used cat to display a file called "gplbrownies.txt" onto the screen, and only wanted to display the line with the string "Linux Rocks" on it, my command would be this:

cat gplbrownies.txt | grep "Linux Rocks"

The above command would display only the line of text that contained the sequence "Linux Rocks". It should be noted that unless you specify otherwise, the search will be case sensitive. To make the search case-insensitive, use the "-i" option, like so:

cat gplbrownies.txt | grep -i "linux rocks"

There still is one more usage of grep that you should be familiar with. You can invoke grep to look for a pattern in a file with the simple syntax of:

grep <pattern> <filename>

NOTE: This will only look for the pattern in the specified filename. This will NOT work with commands like "dmesg" and "ps". So, to use another example, if I was searching for the string "Alex Pearsall" in the text file "linuxstaff.txt", I would type:

grep "Alex Pearsall" linuxstaff.txt

and that would display all lines with the string "Alex Pearsall" in them. If you're dealing with a very long text file, you may find it handy for grep to print the line number along with any matching text. Just use the "-n" flag, like so:

grep -n "Alex Pearsall" linuxstaff.txt 19:Alex Pearsall, Writer (FirstStep)

Suppose that you wanted to look for every line in a file that did NOT contain the text "Alex Pearsall". Well, simply use the "inverse" option, "-v". The usage is simple:

grep -v "Alex Pearsall" linuxstaff.txt

Now some of this might not seem useful to you now, but as you progress and familiarize yourself with the "ps", or process viewing function, and you only want to find processes with a certain title so you can kill them, a command like grep is INVALUABLE. For example:

ps ax | grep netscape

would return the process ID (PID) of the "netscape" process, which we could then kill with the kill command. Additionally, when you start looking through log files for certain messages and such, grep is the most time-effective way for you to do this. Feel free to "man grep", or use it with the "--help" option, to get a better idea of what it is capable of.

On a final note, you may recall that the "re" in "grep" stands for Regular Expression. A regular expression is a special way of pinpointing exactly what you are searching for, and allows you to use special characters to accomplish this. Regular expressions can be very complicated, and entire books have been written solely for the purpose of helping people to master them. For now, practice using grep with simple search strings. Keep watching the FirstStep section, though, because we'll soon be featuring an article devoted to helping you learn the basics of regular expressions. They can be used with many commands and applications, not just grep, so they'll come in handy more often than you would think.

I hope you got something from this article. If you have any questions, drop me a line at rebelpacket@linux.com.





   Page 1 of 1