3.5 grep command



3.5.1 grep command

The grep command searches data from files. Moreover, it is also possible to retrieve from the data of the standard input by using "| grep".

Format

grep [option] condition [file]


The file specification can be not only single file but also multiple file specified by wild-card like '*'. Wildcards are symbols used to express unspecified character strings.

The regular expression is used as a search condition. Regular expression is a very important function that are used not only for grep but also for pattern matching in UNIX and other programming languages.


Regular expression is an expression method that gives advanced search conditions by using meaningful symbols as well as character strings. The main symbols used are as follows.


Symbol Meaning
^ Represents the beginning of a line
$ Represent the end of the line
. Means an arbitrary character
* Means arbitrary character sets
[...] Means any one of ...
[^...] Means that a character of ... is not included
\ Escape the regular expression symbols


^a All lines beginning with a
b$ All lines ending in b
a.b One arbitrary character between a and b
[ab]ab Beginning with a or b, and string ab follows (aab, bab, ...)
[^ab]ab Beginning with neither a nor b, and string ab follows (xab, zab, ...)

Practice: String search by grep


$ grep abc /etc/* ↵
       : (Display files which are in the /etc directory and contains the character string abc.)
       :
/etc/services:abcvoice-port 3781/tcp
       :
$ grep ^xy /etc/* ↵
       : (Display files which are in the /etc directory and contains the line beginning with xy.)
       :
/etc/services:xyplex-mux 173/tcp
       :



Multiple search conditions can be specified by OR, or negative conditions can be specified.


Options

-e
Specified character string is treated as search pattern.
-i
Do not distinguish between capital letters and small letters in both search patterns and input files.
-v
The lines which did not match search pattern are chosen.



Practice: Search strings by using options


$ grep -e abc -e xyz /etc/* ↵
(Display the lines which contain 'abc' or 'xyz' string in files under /etc directory.)
       :
/etc/mime.types:chemical/x-xyz    xyz
       :
/etc/services:abcvoice-port 3781/tcp
       :
$ grep -i hostname /etc/* ↵
(Display the lines which contain 'hostname' string(either capital letters or small characters) in files under /etc directory.)
       :
rc.sysinit:    HOSTNAME=localhost
       :
rc.sysinit:    # Reset the hostname.
       :
$ grep abc /etc/* | grep -v tcp↵
(Display the lines which contain 'abc', not contain 'tcp', in files under /etc directory.)
       :
services:abcvoice-port    3781/udp       # ABCvoice server port
       :
The line (/etc/services:abcvoice-port 3781/tcp) which was displayed above is not displayed this time, because it contains the exclusion string 'tcp'.


3.5.2 matching with the standard output

The grep command can match not only the contents of files but also the input from the standard input.


Practice: Search the result of the standard output by grep command


$ ls /usr/bin/ | grep d$ ↵        (Find lines which end with 'd' from /usr/bin files list.)
       :
       :
xxd
yppasswd
$


The condition of d$ means that end by d. "ls /usr/bin" output the file list of /usr/bin, therefore it searchs the files which ended by d in the list.



Previous Next