grep command tips
Sometimes only specific lines of a file with certain text on it
need to be seen. The grep command
examines a file and only display lines that contain a certain
string.
$ grep
gcc required_packages.txt
libgcc-3.4.6-10
gcc-3.4.6-10.i386.rpm
gcc-c++-3.4.6-10.i386.rpm
This example displays all the lines in the
required_packages.txt file which contain the string gcc. In this
usage, grep matches the string gcc in only its given lowercase
form. To search for a string without case sensitivity, add the -i
option.
Occasionally, you may want to reverse this search and look for
lines which do not contain a given piece of text. In that case,
use the -v option for grep.
$ grep -v
-i rpm required_packages.txt
REHEL4, OEL4:
Refer to Note 880211.1
binutils-2.15.92.0.2-25
compat-libstdc++-33-3.2.3-47.3
elfutils-libelf-0.97.1-5
glibc-2.3.4-2.41
glibc-common-2.3.4-2.41
libaio-0.3.105-2
...
In this example, the two options -v and -i have been combined
to search without case sensitivity for lines that do not contain
the string rpm. When using multiple options, most Linux commands
will let you combine them after a single hyphen. Entering grep -i
-v is the same as grep -iv.
These are the most common ways file contents may be viewed. In
the next chapter, how to edit files with the popular, though often
maligned, vi editor will be shown.