The purpose of this chapter is to introduce a small number of command line tools which may prove useful for everyday use. Of course, you may skip this chapter if you only intend to use a graphical environment, but a quick glance may change your opinion :-)
There is not really any organization in this chapter. Utilities are listed as they come, from the most commonly used to the most arcane. Each command will be illustrated by an example, but it is left as an exercise to you to find more useful uses of them.
Okay, the name is not very intuitive, neither is its acronym, but its use is simple: looking for a pattern given as an argument in one or more files. Its syntax is:
grep [options] <pattern> [one or more file(s)] |
If several files are mentioned, their name will precede each matching line displayed in the result. Use the -h option to prevent the display of these names; use the -l option to get nothing but the matching filenames. It can be useful, especially in long argument lists, to browse the files with a shell loop and use the grep <pattern> <filename> /dev/null trick. The pattern is a regular expression, even though most of the time it consists of a simple word. The most frequently used options are the following:
-i: Make a case insensitive search.
-v: Invert search: display lines which do not match the pattern.
-n: Display the line number for each line found.
-w: Tells grep that the pattern should match a whole word.
Here's an example of how to use it:
$ cat my_father Hello dad Hi daddy So long dad # Search for the string "hi", no matter the case $ grep -i hi my_father Hi daddy # Search for "dad" as a whole word, and print the # line number in front of each match $ grep -nw dad my_father 1:Hello dad 3:So long dad # We want all lines not beginning with "H" to match $ grep -v "^H" my_father So long dad $ |
In case you want to use grep in a pipe, you don't have to specify the filename as, by default, it takes its input from the standard input. Similarly, by default, it prints the results on the standard output, so you can pipe the output of a grep to yet another program without fear. Example :
$ cat /usr/doc/HOWTO/Parallel-Processing-HOWTO | \ grep -n thread | less |