linux文本内容过滤命令(grep)

grep 命令

用途:用于打印输出符合条件字符串所在的行

格式:

grep [选项]...  ""查找条件"   目标文件

常用选项:

① -i : 忽略大小写

② -v:反选,用于查找不满足条件的字符串所在行

③-n :输出满足条件字符串所在行的行号

④--color:颜色凸显满足条件的字符串

查找条件:

①:字符串,表示匹配字符串

②:^字符串  , 表示匹配 以这个字符串开头

③:字符串$, 表示匹配以这个字符串结尾

④:^$,表示匹配 空行

⑤:正则表达式

        [a-z] :表示匹配小写字母 a 到 z

        [A-Z] :表示匹配大写字母 A 到 Z

        [a-zA-Z] :表示匹配英文字母,无论大小写都行

        [0-9]:表示匹配数字 0 到 9

       . :表示任意一个字符

例子1:匹配 /etc/passwd 文件中,含有 "nologin" 字符串所在的行

[root@www etc]# grep --color "nologin" /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
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
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/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
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/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
[root@www etc]# 

例子2:输出 /etc/passwd 文件中,以 "a"开头的行, 同时输出行号

[root@www etc]# grep -n "^a" /etc/passwd
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
21:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
22:abrt:x:173:173::/etc/abrt:/sbin/nologin
26:apache:x:48:48:Apache:/var/www:/sbin/nologin
[root@www etc]# 

例子3:输出 /etc/passwd 文件中 , 以"bash"结尾的行

[root@www etc]# grep "bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
mysql:x:496:493:MySQL server:/var/lib/mysql:/bin/bash
[root@www etc]# 

例子4:输出ifconfig 命令中非空行内容

[root@www etc]# ifconfig                                
eth0      Link encap:Ethernet  HWaddr 00:0C:29:51:E8:31  
          inet addr:192.168.174.151  Bcast:192.168.174.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe51:e831/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2936 errors:0 dropped:0 overruns:0 frame:0
          TX packets:375 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:193685 (189.1 KiB)  TX bytes:71316 (69.6 KiB)

lo        Link encap:Local Loopback  
          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:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b)

[root@www etc]# ifconfig | grep -v "^$"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:51:E8:31  
          inet addr:192.168.174.151  Bcast:192.168.174.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe51:e831/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3022 errors:0 dropped:0 overruns:0 frame:0
          TX packets:407 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:200511 (195.8 KiB)  TX bytes:75816 (74.0 KiB)
lo        Link encap:Local Loopback  
          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:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b)
[root@www etc]# 

例子5:输出 /etc/passwd 文件中 , r 开头中间两个字符任意以 t 结尾的字符串

[root@www etc]# grep --color "r..t" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@www etc]# 

 

你可能感兴趣的:(技术,运维,linux,grep)