本文中主要介绍了linu系统下grep命令、正则表达式的基本参数和使用格式、方法:
grep的理解:
文本搜索工具,根据用户指定的文本模式对目标文件进行逐行搜索,显示能够被模式所匹配到的行;
grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来;
egrep命令是一个搜索文件获得模式,使用该命令可以任意搜索文件中的字符串和符号,也可以为你搜索一个多个文件的字符串,一个提示符可以是单个字符、一个字符串、一个字、一个句子;
fgrep 命令搜索 File 参数指定的输入文件(缺省为标准输入)中的匹配模式的行。fgrep 命令特别搜索 Pattern 参数,它们是固定的字符串。如果在 File 参数中指定一个以上的文件 fgrep 命令将显示包含匹配行的文件。
命令格式: grep [options] ‘PATTERN’ file,…
基本正则表达式:
正则表达式就是一类字符所书写出的模式(pattern),来处理字符串的方法,它是以行为单位来进行字符串的处理行为,正则表达式通过一些特殊称号的辅助,可以让用户轻易达到查找、删除、替换某特定字符串的处理程序。
1. grep 常用选项:
-v: 反向查找,显示不能被模式所匹配到的行;
-o: 仅显示被模式匹配到的字串,而非整行;
-i: 不区分字符大小写;
-E: 支持扩展的正则表达式;
--color=auto:符合条件后以颜色显示
-A #:显示匹配到字符的那行的后面n行;
-B #:显示匹配到字符的那行的前面n行;
-C #:显示匹配到字符的那行的前后n行;
有时会要用到管道 | 吆!!
下面是 常用选项 应用举例:
1).查看/etc/fstab文件中以非#号开头的行--># grep -v "^#" /etc/inittab
1
2
3
|
[root@localhost ~]
# grep -v "^#" /etc/inittab
id
:3:initdefault:
[root@localhost ~]
#
|
1
2
3
4
5
6
7
8
9
10
|
[root@localhost ~]
# grep -o "root" /etc/passwd
root
root
root
root
root
root
root
root
[root@localhost ~]
#
|
1
2
3
4
5
|
[root@localhost ~]
# grep -i "^root" /etc/passwd
root:x:0:0:root:
/root
:
/bin/bash
rooter:x:500:500::
/home/rooter
:
/bin/bash
ROOT:x:501:501::
/home/ROOT
:
/bin/bash
[root@localhost ~]
#
|
1
2
3
|
[root@localhost ~]
# grep -n "sshd" /etc/passwd
32:sshd:x:74:74:Privilege-separated SSH:
/var/empty/sshd
:
/sbin/nologin
[root@localhost ~]
#
|
1
2
3
4
5
6
|
1
2
3
4
|
[root@localhost ~]
# grep -A 1 "ftp" /etc/passwd
ftp
:x:14:50:FTP User:
/var/ftp
:
/sbin/nologin
nobody:x:99:99:Nobody:/:
/sbin/nologin
[root@localhost ~]
#
|
1
2
3
4
|
[root@localhost ~]
# cat /etc/passwd|grep -B 1 sshd
sshd:x:74:74:Privilege-separated SSH:
/var/empty/sshd
:
/sbin/nologin
[root@localhost ~]
#
|
1
2
3
4
5
6
7
|
[root@localhost ~]
# cat /etc/passwd|grep -C 2 sshd
nfsnobody:x:65534:65534:Anonymous NFS User:
/var/lib/nfs
:
/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:
/var/empty/sshd
:
/sbin/nologin
tcpdump:x:72:72::/:
/sbin/nologin
rooter:x:500:500::
/home/rooter
:
/bin/bash
[root@localhost ~]
#
|
1
2
3
4
5
6
7
|
1
2
3
|
[root@localhost ~]
# grep -n '^[A-Z]' /etc/passwd
35:ROOT:x:501:501::
/home/ROOT
:
/bin/bash
[root@localhost ~]
#
|
1
2
3
4
5
|
[root@localhost ~]
# grep [[:digit:]] xiaoma.txt
1command
a5bxyza5b
a5bxyza6b
[root@localhost ~]
#
|
1
2
3
4
5
6
7
8
9
10
|
[root@localhost ~]
# grep --color=auto "[^[:digit:]]" xiaoma.txt
comand
1command
commmand
commmmand
commmmmand
commmmmmmand
a5bxyza5b
a5bxyza6b
[root@localhost ~]
#
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@localhost ~]
# grep "com*" /etc/man.config
# Generated automatically from man.conf.in by the
# configure script.
# man.conf from man-1.6f
# and man.conf(5).
...
# text twice. (But compressed pages compare unequal.)
# Decompress with given decompressor when input file has given extension
# The command given must act as a filter.
# If MAKEWHATISDBUPDATES variable is uncommented
[root@localhost ~]
#
|
1
2
3
4
5
6
7
|
[root@localhost ~]
# grep "com.*" /etc/man.config
# Certain versions of the FSSTND recommend putting formatted versions
# Certain versions of the FHS recommend putting formatted versions of
...
# The command given must act as a filter.
# If MAKEWHATISDBUPDATES variable is uncommented
[root@localhost ~]
#
|
1
2
3
4
5
6
7
8
|
[root@localhost ~]
# grep 'com\?' /etc/man.config
# Generated automatically from man.conf.in by the
# configure script.
# man.conf from man-1.6f
...
# The command given must act as a filter.
# If MAKEWHATISDBUPDATES variable is uncommented
[root@localhost ~]
#
|
1
2
3
4
5
6
7
8
|
[root@localhost ~]
# grep "com\{2\}" /etc/man.config
# Certain versions of the FSSTND recommend putting formatted versions
# Certain versions of the FHS recommend putting formatted versions of
# Uncomment if you want to include one of these by default
# The command "man -a xyzzy" will show all man pages for xyzzy.
# The command given must act as a filter.
# If MAKEWHATISDBUPDATES variable is uncommented
[root@localhost ~]
#
|
1
2
3
4
5
6
7
|
[root@localhost ~]
# grep "com\{1,3\}" /etc/man.config
# Certain versions of the FSSTND recommend putting formatted versions
...
# Decompress with given decompressor when input file has given extension
# The command given must act as a filter.
# If MAKEWHATISDBUPDATES variable is uncommented
[root@localhost ~]
#
|
1
2
3
4
5
6
|
[root@localhost ~]
# grep "com\{1,\}" /etc/man.config
# Certain versions of the FSSTND recommend putting formatted versions
# Certain versions of the FHS recommend putting formatted versions of
...
# If MAKEWHATISDBUPDATES variable is uncommented
[root@localhost ~]
#
|
1
2
3
4
5
|
[root@localhost ~]
# grep "com\{0,5\}" /etc/man.config
# Generated automatically from man.conf.in by the
...
# If MAKEWHATISDBUPDATES variable is uncommented
[root@localhost ~]
#
|
1
2
3
4
5
6
7
8
9
|
[root@localhost ~]
# grep "^#" /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Feb 9 13:43:51 2014
#
# Accessible filesystems,by reference,are maintained under '/dev/disk'
# See man pages fstab(5),findfs(8),mount(8) and/or blkid(8) for more info
#
[root@localhost ~]
#
|
1
2
3
4
5
6
|
1
2
3
4
5
|
[root@localhost ~]
# grep "\<root" /etc/passwd
root:x:0:0:root:
/root
:
/bin/bash
operator:x:11:0:operator:
/root
:
/sbin/nologin
rooter:x:500:500::
/home/rooter
:
/bin/bash
[root@localhost ~]
#
|
1
2
3
4
5
|