Linux 命令之 grep

1:将/etc/passwd中有root字符的行显示出来

1 [root@bogon zkero]# grep -n 'root' /etc/passwd
2 1:root:x:0:0:root:/root:/bin/bash
3 11:operator:x:11:0:operator:/root:/sbin/nologin

2:指定文件(test1)中读取hello 无论大小写的行

1 [root@bogon zkero]# grep -in 'hello' test1 
2 1:hello
3 3:HELLO

说明:grep 中‘hello’中的'hello'大小写也是忽略的

3:定位符的使用^

/etc/passwd 中只显示首次出现root的行

1 [root@bogon zkero]# grep -n 'root' /etc/passwd
2 1:root:x:0:0:root:/root:/bin/bash
3 11:operator:x:11:0:operator:/root:/sbin/nologin
4 [root@bogon zkero]# grep -n '^root' /etc/passwd
5 1:root:x:0:0:root:/root:/bin/bash

^在[ ]之内是反向选择,在[ ]之外是定位符

4:显示行尾字符所在行

1 [root@bogon zkero]# grep -n 'bash$' /etc/passwd 
2 1:root:x:0:0:root:/root:/bin/bash
3 34:zkero:x:500:500:zkero:/home/zkero:/bin/bash

5:定位显示空白行

 1 [root@bogon zkero]# cat -n test1 
 2      1    hello
 3      2    world
 4      3    HELLO
 5      4    
 6      5    WORLD
 7      6    
 8      7    i an supker
 9      8    i like computer!
10      9    
11 [root@bogon zkero]# grep -n '^$' test1 
12 4:
13 6:
14 9:

6:一般脚本文件都有空白行和#注释开头的行,如果特殊需要,需要省略这些行:

 1 [root@bogon zkero]# cat /etc/sysctl.conf 
 2 # Kernel sysctl configuration file for Red Hat Linux
 3 #
 4 # For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and
 5 # sysctl.conf(5) for more details.
 6 
 7 # Controls IP packet forwarding
 8 net.ipv4.ip_forward = 0
 9 
10 # Controls source route verification
11 net.ipv4.conf.default.rp_filter = 1
12 
13 # Do not accept source routing
14 net.ipv4.conf.default.accept_source_route = 0
15 
16 # Controls the System Request debugging functionality of the kernel
17 kernel.sysrq = 0
18 
19 # Controls whether core dumps will append the PID to the core filename.
20 # Useful for debugging multi-threaded applications.
21 kernel.core_uses_pid = 1
22 
23 # Controls the use of TCP syncookies
24 net.ipv4.tcp_syncookies = 1
25 
26 # Disable netfilter on bridges.
27 net.bridge.bridge-nf-call-ip6tables = 0
28 net.bridge.bridge-nf-call-iptables = 0
29 net.bridge.bridge-nf-call-arptables = 0
30 
31 # Controls the default maxmimum size of a mesage queue
32 kernel.msgmnb = 65536
33 
34 # Controls the maximum size of a message, in bytes
35 kernel.msgmax = 65536
36 
37 # Controls the maximum shared segment size, in bytes
38 kernel.shmmax = 68719476736
39 
40 # Controls the maximum number of shared memory segments, in pages
41 kernel.shmall = 4294967296
42 [root@bogon zkero]# grep -v '^$' /etc/sysc
43 sysconfig/   sysctl.conf  
44 [root@bogon zkero]# grep -v '^$' /etc/sysctl.conf | grep -v '^#'
45 net.ipv4.ip_forward = 0
46 net.ipv4.conf.default.rp_filter = 1
47 net.ipv4.conf.default.accept_source_route = 0
48 kernel.sysrq = 0
49 kernel.core_uses_pid = 1
50 net.ipv4.tcp_syncookies = 1
51 net.bridge.bridge-nf-call-ip6tables = 0
52 net.bridge.bridge-nf-call-iptables = 0
53 net.bridge.bridge-nf-call-arptables = 0
54 kernel.msgmnb = 65536
55 kernel.msgmax = 65536
56 kernel.shmmax = 68719476736
57 kernel.shmall = 4294967296

7: 限定连续RE字符范围{ } 

找两个连续的o字符

 1 [zkero@bogon ~]$ cat test2
 2 mygod!
 3 good morning!
 4 eat food!
 5 gooooodbye!
 6 
 7 [zkero@bogon ~]$ grep -n 'o\{2\}' test2 
 8 2:good morning!
 9 3:eat food!
10 4:gooooodbye!

其中:{ } 为特殊符号,需要用\转义

查找连续字符后面接特定字符:比如:3到5个连续字符o后面接d

1 [zkero@bogon ~]$ grep -n 'o\{3,5\}d' test2 
2 4:gooooodbye!

查找g开头,d结尾,指定o数目的例子:

1 [zkero@bogon ~]$ grep -n 'go\{2,5\}d' test2 
2 2:good morning!
3 4:gooooodbye!
4 [zkero@bogon ~]$ grep -n 'go\{2,4\}d' test2 
5 2:good morning!

可知:第4行有5个o,指定2到4个范围,并没有出现第4行内容

 

posted on 2015-06-22 01:36 雪山看雪 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/zker/p/4592779.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值