linux常用命令-grep,egrep,regexp
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux常用命令-grep,egrep,regexp相关的知识,希望对你有一定的参考价值。
grep: 根据模式搜索文本,并将符合模式的文本行显示出来。
Pattern: 文本字符和正则表达式的元字符组合而成匹配条件
grep [options] PATTERN [FILE...]
-i(忽略大小写)
[[email protected] ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]#
--color(加颜色)
-v: 显示没有被模式匹配到的行
[[email protected] ~]# grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
-o:只显示被模式匹配到的字符串
[[email protected] ~]# grep -o root /etc/passwd
root
root
root
root
[[email protected] ~]#
*: 任意长度的任意字符
?: 任意单个字符
[]:
[^]:
正则表达式:REGular EXPression, REGEXP
元字符:
.: 匹配任意单个字符
[]: 匹配指定范围内的任意单个字符
[^]:匹配指定范围外的任意单个字符
字符集合:[:digit:], [:lower:], [:upper:], [:punct:], [:space:], [:alpha:], [:alnum:]
[[email protected] ~]# grep ‘[[:digit:]]$‘ /etc/inittab
# 5 - X11
[[email protected] ~]#
匹配次数(贪婪模式):
*:匹配其前面的字符任意次
a, b, ab, aab, acb, adb, amnb
a*b, a?b
a.*b
[[email protected] ~]# grep ‘a*b‘ test.txt
b
ab
aab
acb
adb
acdafb
asdflkbSDFb
[[email protected] ~]#
.*: 任意长度的任意字符
[[email protected] ~]# grep ‘a.*b‘ test.txt
ab
aab
acb
adb
acdafb
asdflkbSDFb
[[email protected] ~]#
\?: 匹配其前面的字符1次或0次
[[email protected] ~]# grep ‘a\?b‘ test.txt
b
ab
aab
acb
adb
acdafb
asdflkbSDFb
[[email protected] ~]#
\{m,n\}:匹配其前面的字符至少m次,至多n次
\{1,\}:至少一次
\{0,3\}:0-3次
[[email protected] ~]# grep ‘a\{1,3\}b‘ test.txt
ab
aab
[[email protected] ~]#
[[email protected] ~]# grep ‘a.\{1,3\}b‘ test.txt
aab
acb
adb
acdafb
[[email protected] ~]#
位置锚定:
^: 锚定行首,此字符后面的任意内容必须出现在行首
[[email protected] ~]# grep ‘^r..t‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]#
$: 锚定行尾,此字符前面的任意内容必须出现在行尾
[[email protected] ~]# grep ‘login$‘ /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
^$: 空白行
[[email protected] ~]# grep ‘^$‘ .bashrc
[[email protected] ~]# grep ‘^$‘ .bashrc | wc -l
4
[[email protected] ~]#
\<或\b: 锚定词首,其后面的任意字符必须作为单词首部出现
[[email protected] ~]# grep ‘root\>‘ test2.txt
this is root
the user is mroot
chroot is a command
mroot is not a word
[[email protected] ~]#
[[email protected] ~]# grep ‘root\b‘ test2.txt
this is root
the user is mroot
chroot is a command
mroot is not a word
[[email protected] ~]#
\>或\b: 锚定词尾,其前面的任意字符必须作为单词的尾部出现
[[email protected] ~]# grep ‘\<root‘ test2.txt
this is root
rooter is a dog
[[email protected] ~]#
[[email protected] ~]# grep ‘\broot‘ test2.txt
this is root
rooter is a dog
[[email protected] ~]#
\<root>\:锚定单词
[[email protected] ~]# grep ‘\<root\>‘ test2.txt
this is root
[[email protected] ~]#
分组:
\(\)
\(ab\)*
后向引用
\1: 引用第一个左括号以及与之对应的右括号所包括的所有内容
\2:
\3:
He love his lover.
She like her liker.
He like his lover.
l..e
[[email protected] ~]# grep ‘\(l..e\).*\1‘ test3.txt
He love his lover.
She like her liker.
[[email protected] ~]#
练习:
1、显示/proc/meminfo文件中以不区分大小的s开头的行;
grep -i ‘^s‘ /proc/meminfo
grep ‘^[sS]‘ /proc/meminfo
2、显示/etc/passwd中以nologin结尾的行;
grep ‘nologin$‘ /etc/passwd
取出默认shell为/sbin/nologin的用户列表
grep "nologin$‘ /etc/passwd | cut -d: -f1
取出默认shell为bash,且其用户ID号最小的用户的用户名
grep ‘bash$‘ /etc/passwd | sort -n -t: -k3 | head -1 | cut -d: -f1
3、显示/etc/inittab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行;
grep "^#[[:space:]]\{1,\}[^[:space:]]" /etc/inittab
4、显示/etc/inittab中包含了:一个数字:(即两个冒号中间一个数字)的行;
grep ‘:[0-9]:‘ /etc/inittab
5、显示/boot/grub/grub.conf文件中以一个或多个空白字符开头的行;
grep ‘^[[:space:]]\{1,\}‘ /boot/grub/grub.conf
6、显示/etc/inittab文件中以一个数字开头并以一个与开头数字相同的数字结尾的行;
grep ‘^\([0-9]\).*\1$‘ /etc/inittab
练习:
1、找出某文件中的,1位数,或2位数;
grep ‘[0-9]\{1,2\}‘ /proc/cpuinfo
grep --color ‘\<[0-9]\{1,2\}\>‘ /proc/cpuinfo
2、找出ifconfig命令结果中的1-255之间的整数;
3、查找当前系统上名字为student(必须出现在行首)的用户的帐号的相关信息, 文件为/etc/passwd
grep ‘^student\>‘ /etc/passwd | cut -d: -f3
id -u student
REGEXP:REGular EXPression
Pattern:
正则表达式:
Basic REGEXP:基本
Extended REGEXP:扩展
基本正则表达式:
.:
[]:
[^]:
次数匹配:
*:
\?: 0或1次
\{m,n\}:至少m次,至多n次;
.*:
锚定:
^:
$:
\<, \b:
\>, \b:
\(\)
\1, \2, \3, ...
grep:使用基本正则表达式定义的模式来过滤文本的命令;
-i
-v
-o
--color
-E: 使用扩展正则表达式
-A #:
[[email protected] ~]# grep -A 2 ‘^core id‘ /proc/cpuinfo
core id : 0
cpu cores : 1
apicid : 0
[[email protected] ~]#
-B #:
[[email protected] ~]# grep -B 2 ‘^core id‘ /proc/cpuinfo
physical id : 0
siblings : 1
core id : 0
-C #:
[[email protected] ~]# grep -C 2 ‘^core id‘ /proc/cpuinfo
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
[[email protected] ~]#
扩展正则表达式:
字符匹配:
.
[]
[^]
次数匹配:
*:
?:
+: 匹配其前面的字符至少1次
{m,n}
位置锚定:
^
$
\<
\>
分组:
():分组
\1, \2, \3, ...
或者
|: or
C|cat: Cat或cat, C或cat
[[email protected] ~]# grep -E ‘C|cat‘ test6.text
Cat
cat
C
China
[[email protected] ~]#
[[email protected] ~]# grep -E ‘(C|c)at‘ test6.text
Cat
cat
[[email protected] ~]#
[[email protected] ~]# grep -E ‘^[[:space:]]+‘ /boot/grub/grub.conf
root (hd0,0)
kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=UUID=57d85756-7680-4c7c-9125-6ad67dae2c45 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM quiet
initrd /initramfs-2.6.32-504.el6.x86_64.img
[[email protected] ~]#
grep -E = egrep
4、显示所有以数字结尾且文件名中不包含空白的文件;
ls *[^[:space:]]*[0-9] ?????????
找出/boot/grub/grub.conf文件中1-255之间的数字;
\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>
\.
ifconfig | egrep ‘\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>‘
ifconfig | egrep --color ‘(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>‘
IPv4:
5类:A B C D E
A:1-127
B:128-191
C:192-223
\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[01][0-9]|22[0-3])\>(\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>){2}\.\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>
http://mageedu.blog.51cto.com/
grep, egrep
fgrep: 不支持正则表达式
以上是关于linux常用命令-grep,egrep,regexp的主要内容,如果未能解决你的问题,请参考以下文章