您的位置:首页 > 运维架构 > Linux

Linux基础命令:grep 必知必会

2016-03-12 08:43 531 查看

grep(Global regular expression print)

grep searches the input files for lines containing a match to a given pattern list. When it finds a match in a line, it copies the line to standard output (by default), or whatever other sort of output you have requested with options. Though grep expects to do the matching on text, it has no limits on input line length other than available memory, and it can match arbitrary characters within a line. If the final byte of an input file is not a newline, grep silently supplies one. Since newline is also a separator for the list of patterns, there is no way to match newline characters in a text.

Invoking grep

The general synopsis of the grep command line is
grep options pattern  input_file_name
There can be zero or more options. pattern will only be seen as such (and not as an input_file_name) if it wasn't already specified within options (by using the -e pattern or -f file options). There can be zero or more input_file_names.

grep options

Matching Control

-e pattern , --regexp=pattern

This can be used to specify multiple search patterns, or to protect a pattern beginning with a '-' (-e is specified by POSIX)
-f file , --file=file

Obtain patteerns from file, one per line. (-f is specified by POSIX)
-i , --ignore-case

Ignore case distinctions. (-i is specified by POSIX)
-v , --invert-match

Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX)
-w , --word-regexp

Select only those lines containing matches that form whole words.

-x , --line-regexp

Select only those matches that exactly match the whole line. For a eregular expression pattern, this is like parenthesizing the pattern and the surrounding it with '^' and '$'.

General Output Control
-c , -count

Suppress normal output, instead print a count of matching lines for each input file. With the -v option, count non-matching lines.

-o , --only-matching

Print only the matched(non-empty) parts of matching lines.
-q , --quiet , --silent

Quiet, do not write anything to standard ouput. Exit immediately with zeron status if any match is found, even if an error was detected.
-Z , --null

Output a zero byte(the ASCII NUL character) intead of the character that normally follows a file name. For example, ‘grep -lZ’ outputs a zero byte after each file name instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be used with commands like ‘find -print0’, ‘perl -0’, ‘sort -z’, and ‘xargs -0’ to process arbitrary file names, even those that contain newline characters.
Context Line Control
-A num , --after-context=num

Print num lines of trailing context after matching lines.
-B num , --before-context=num

Print num lines of leading context before matching lines.

-C num , --context=num

Print num lines of leading and trailing output context.

grep programs

-E

--extended-regexp
Interpret the pattern as an extended regular expression (ERE). (-E is specified by POSIX.)
-F

--fixed-strings
Interpret the pattern as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched. (-F is specified by POSIX.)
-P

--perl-regexp
Interpret the pattern as a Perl regular expression. This is highly experimental and ‘grep -P’ may warn of unimplemented features.

grep example

## Ex: Search multiple patterns using -e option
[zbj@localhost ~]$ grep  -e 'root' -e 'zbj' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
zbj:x:1000:1000::/home/zbj:/bin/bash

## Ex: Ignore case using -i option
[zbj@localhost ~]$ grep -i 'FTP' /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

## Ex: Invert match using -v option
[zbj@localhost ~]$ grep -v bash /etc/passwd | grep -v nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

## Ex: Count the number of matching patterns using -c option
[zbj@localhost ~]$ grep -c 'nologin' /etc/passwd
18

## Ex: Display N number of lines before & after pattern matching
[zbj@localhost ~]$ grep -B 3 'games' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

[zbj@localhost ~]$ grep -A 3 'games' /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin

[zbj@localhost ~]$ grep -C 3 'games' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息