码迷,mamicode.com
首页 > 其他好文 > 详细

正则(grep)

时间:2018-01-16 00:38:25      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:systemctl   emctl   config   tor   ftp   long   var   过滤   gre   

正则简介:

技术分享图片

grep

技术分享图片
(1)基础用法

[root@weix01 grep]# grep ‘nologin‘ 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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

(2)-c 行数

[root@weix01 grep]# grep -c ‘nologin‘ passwd
16

(3)-n 行号

[root@weix01 grep]# grep -n ‘nologin‘ passwd
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

(4)-i 不区分大小写

[root@weix01 grep]# grep -ni ‘nologin‘ passwd
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/Nologin

(5)-v 取反

[root@weix01 grep]# grep -v ‘nologin‘ passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/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
weixing01:x:1000:1000::/home/weixing01:/bin/bash
weixing02:x:1002:1002::/home/weixing02:/bin/bash
user5:x:1006:1006::/home/user5:/bin/bash

(6)-r 遍历所有子目录

[root@weix01 grep]# grep  ‘root‘ /etc
grep: /etc: 是一个目录
[root@weix01 grep]# grep -r ‘root‘ /etc
/etc/grub.d/00_header:datarootdir="/usr/share"
/etc/grub.d/00_header:export TEXTDOMAINDIR="${datarootdir}/locale"
/etc/grub.d/00_header:if loadfont `make_system_path_relative_to_its_root "${GRUB_FONT}"` ; then
/etc/grub.d/00_header:    font="`make_system_path_relative_to_its_root "${font_path}"`"

(7)-A 后面跟数字,过滤出符合要求的行以及该行下面两行

[root@weix01 grep]# grep -nA2 ‘root‘ passwd
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

(8)-B后面跟数字,过滤出符合要求的行以及该行上面两行

1:root:x:0:0:root:/root:/bin/bash
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin

(9)-C后面跟数字,过滤出符合要求的行以及该行上下两行

[root@weix01 grep]# grep -nC2 ‘root‘ passwd
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

正则表达式

1.过滤数字

[root@weix01 grep]# grep ‘[0-9]‘ passwd               #过滤数字
root:x:0:0:root:/root:/bin/bash
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
weixing01:x:1000:1000::/home/weixing01:/bin/bash
weixing02:x:1002:1002::/home/weixing02:/bin/bash
weixing04:x:1005:1002::/home/weix111:/sbin/nologin
user5:x:1006:1006::/home/user5:/bin/bash

2.过滤以某个字符开头的行

[root@weix01 grep]# grep -n ‘^#‘ inittab 
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:#
5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6:#
7:# systemd uses ‘targets‘ instead of runlevels. By default, there are two main targets:
8:#
9:# multi-user.target: analogous to runlevel 3
10:# graphical.target: analogous to runlevel 5
12:# To view current default target, run:
13:# systemctl get-default
14:#
15:# To set a default target, run:
16:# systemctl set-default TARGET.target
17:#
[root@weix01 grep]# grep -nv ‘^#‘ inittab 
11:llsljgijlsjgisjglisg

3.括号里面的^字符:取非

[root@weix01 grep]# grep ‘^[^0-9]‘ inittab                     以非数字开头
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses ‘targets‘ instead of runlevels. By default, there are two main targets:
@#@$%!$!@
adsada231 4
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
llsljgijlsjgisjglisg
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
[root@weix01 grep]# grep -v ‘^[^0-9]‘ inittab             #以数字开头
12131341241
14aaeq

4.表示任意一个字符.

[root@weix01 grep]# grep ‘r.o‘ passwd       #  .表示一个字符
root:x:0:0:root:/root:/bin/bash
224234ijlkjdiajfr1oafa
2222r>o23ijao
operator:x:11:0:operator:/root:/sbin/nologin

5.*

[root@weix01 grep]# grep ‘o*o‘ passwd       #与左面字符绑定,表示左面字符重复0到n次
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
224234ijlkjdiajfr1oafa
2222r>o23ijao
afrtyyoaflarttoafl
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

6.所有都匹配: .*

[root@weix01 grep]# grep ‘.*‘ passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
224234ijlkjdiajfr1oafa
2222r>o23ijao
afrtyyoaflarttoafl
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

7.{}

[root@weix01 grep]# grep ‘o\{2\}‘ passwd       #o出现两次的,但是需要脱意
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

8.egrep :{}使用egrep不用脱意

[root@weix01 grep]# egrep ‘o{2}‘ passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

9.grep -E =egrep
10.+:表示+前面字符1次或多次 没有0

[root@weix01 grep]# egrep ‘o+o‘ passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

11.? :?前面字符0次或者1次

[root@weix01 grep]# egrep ‘o?t‘ passwd
root:x:0:0:root:/root:/bin/bash
afrtyyoaflarttoafl
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

12.| 表示或者的意思

[root@weix01 grep]# egrep ‘root|nologin‘  passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

13.一个组合重复几次:

[root@weix01 grep]# egrep ‘(oo){2}‘  passwd
roooot:x:0:0:root:/root:/bin/bash

正则(grep)

标签:systemctl   emctl   config   tor   ftp   long   var   过滤   gre   

原文地址:http://blog.51cto.com/13517254/2061315

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!