您的位置:首页 > 其它

L2:grep使用正则表达式

2015-08-28 17:03 423 查看
本文对grep的使用与正则表达式做相关总结(包含实际演示例子)
1、总结所涉及命令的使用方法及相关示例展示;
命令:
权限管理:
权限管理: chmod
所属关系管理: chown, chgrp
文件遮罩码:umask
Linux文本处理三剑客:
grep: 文本过滤工具;
sed:文本编辑器(行);stream editor
awk:文本报告生成器;Linux上awk的实现为gawk

grep作用:文本搜索工具,根据用户指定的“模式(pattern)”逐行去搜索目标文本,打印匹配到的行;模式:由正则表达式的元字符及文本字符所编写的过滤条件;
元字符:字符不表示其字面意义,而用于表示通配或控制功能;
分两类:基本正则表达式:BRE扩展正则表达式:EREgrep [OPTIONS] PATTERN [FILE...]:选项:--color=auto:对匹配到的串做高亮显示;-v:显示模式匹配不到行;-i: 忽略字符大小写;-o: 仅显示能够被模式匹配到的串本行;-q: 静默模式;(常用语脚本判断,有输出$?为0,没输出$?不为0)-E:使用扩展的正则表达式;实例演示:
#
[root@zy03 ~]# grep --color 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
#不包含nologin行 (-v选项)
[root@zy03 ~]# grep --color -v 'nologin' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
a:x:500:531::/home/a:/bin/bash
bash:x:501:532::/home/bash:/bin/bash
testbash:x:502:533::/home/testbash:/bin/bash
basher:x:503:534::/home/basher:/bin/bash
centos:x:505:536::/home/centos:/bin/bash
user1:x:506:537::/home/user1:/bin/bash
#忽略大小写 -i
[root@zy03 ~]# grep --color -i 'ROOT' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
#仅显示匹配的行
[root@zy03 ~]# grep --color -o 'root' /etc/passwd
root
root
root
root
2、总结基本正则表达式及扩展正则表达式

基本正则表达式的元字符:字符匹配:.: 匹配任意单个字符;[]:匹配指定范围内的任意单个字符;[^]:匹配指定范围内的任意单个字符;

[:lower:], [:upper:], ...

次数匹配:用于要指定其次数的字符的后面;*: 任意次;\?:0或1次;grep "x\?y"

\+:1或多次; \{m\}:精确限制为m次;\{m,n\}: 至少m次,至多n次,[m,n]\{0,n\}:至多n次;\{m,\}:至少m次;

.*: 匹配任意长度的任意字符;

位置锚定:^: 行首锚定;用于模式的最左侧;$: 行尾锚定;用于模式的最右侧;\<, \b: 词首锚定;用于表示单词的模式的左侧;\>, \b:词尾锚定;用于表示单词的模式的右侧;^$: 空白行;

分组:\(\)

分组的小括号中的模式匹配到的内容,会在执行过程中被正则表达式引擎记录下来,并保存内置的变量中;这些变量分别是\1, \2, ...\1: 从左侧起,第一个左括号,以及与之配对的右括号中间的模式所匹配到的内容;\2:如上类推...后向引用:使用变量引用前面的分组括号中的模式所匹配到的字符;
扩展的正则表达式:grep家庭有三个命令:grep:基本正则表达式-E: 扩展正则表达式-F:不支持正则表达式egrep:扩展正则表达式fgrep:不支持正则表达式

扩展正则表达式的元字符:字符匹配:.: 任意单个字符[]:[^]:

次数匹配:*?: 0次或1次;+: 1次以上;{m}: 精确匹配m次;{m,n}: 至少m次,至多n次;

锚定:^: 锚定行首$: 锚定行尾\<, \b\>, \b

分组:()后向引用:\1, \2, ...或者:
a|b C|cat: 不表示Cat或cat,而表示C或cat;要写成(C|c)at3,显示/etc/passwd文件中以bash结尾的行

egrep '\<bash\>$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
a:x:500:531::/home/a:/bin/bash
bash:x:501:532::/home/bash:/bin/bash
testbash:x:502:533::/home/testbash:/bin/bash
basher:x:503:534::/home/basher:/bin/bash
centos:x:505:536::/home/centos:/bin/bash
user1:x:506:537::/home/user1:/bin/bash
4、显示/etc/passwd文件中的两位数或三位数
cat /etc/passwd|grep '[0-9]\{2,3\}'|grep -v '[0-9]\{4,\}'
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
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
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
a:x:500:531::/home/a:/bin/bash
bash:x:501:532::/home/bash:/bin/bash
testbash:x:502:533::/home/testbash:/bin/bash
basher:x:503:534::/home/basher:/bin/bash
nologin:x:504:535::/home/nologin:/sbin/nologin
centos:x:505:536::/home/centos:/bin/bash
user1:x:506:537::/home/user1:/bin/bash
5、显示`netstat -tan`命令结果中以‘LISTEN’后跟0个、1个或者多个空白字符结尾的行
1,netstat -tan|grep 'LISTEN[[:space:]]\{0,\}'
2,netstat -tan|egrep 'LISTEN[[:space:]]?+$'

tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:59226               0.0.0.0:*                   LISTEN
tcp        0      0 :::111                      :::*                        LISTEN
tcp        0      0 :::22                       :::*                        LISTEN
tcp        0      0 ::1:631                     :::*                        LISTEN
tcp        0      0 ::1:25                      :::*                        LISTEN
tcp        0      0 :::47009                    :::*                        LISTEN
6、添加用户bash、testbash、basher以及nologin用户(nologin用户的shell为/sbin/nologin);而后找出/etc/passwd文件中用户名与其shell名相同的行
egrep --color '(\b^[[:alnum:]]+\b).*\1$' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:501:532::/home/bash:/bin/bash
nologin:x:504:535::/home/nologin:/sbin/nologin
7、显示当前系统上root、centos或者user1用户的默认shell和UID (请事先创建这些用户,若不存在)
grep -E '^root|^centos|^user1' /etc/passwd|cut -d: -f1,3,7
root:0:/bin/bash
centos:505:/bin/bash
user1:506:/bin/bash
8、找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行
grep '.*()' /etc/rc.d/init.d/functions
echo_passed() {
echo_warning() {grep '.*()' /etc/rc.d/init.d/functions
fstab_decode_str() {
checkpid() {
__readlink() {
__fgrep() {
__umount_loop() {
__umount_loopback_loop() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
confirm() {
get_numeric_dev() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {
key_is_random() {
find_crypto_mount_point() {
init_crypto() {
9、使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用egrep取出其目录名
echo "/etc/sysconfig/network" |egrep --color -o '[[:alnum:]]+$'
network
echo "/etc/sysconfig/network"|egrep --color -o '.*/'
/etc/sysconfig/
10,找出ifconfig命令执行结果中1-255之间的数字
ifconfig |egrep --color=auto "\<[12][0-9][0-9]\>|\<[1-9][0-9]\>|\<[1-9]\>"
eth0      Link encap:Ethernet  HWaddr 52:54:00:A3:D2:58
inet addr:192.168.0.96  Bcast:192.168.0.255  Mask:255.255.255.0
inet6 addr: fe80::5054:ff:fea3:d258/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX bytes:117052479 (111.6 MiB)  TX bytes:3472613 (3.3 MiB)
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:16436  Metric:1
RX packets:44 errors:0 dropped:0 overruns:0 frame:0
TX packets:44 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:2990 (2.9 KiB)  TX bytes:2990 (2.9 KiB)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: