grep小练习

简介:

1、显示/proc/meminfo文件中以大写s开头的行,要求使用两种方式

2、显示/etc/passwd文件中不以/bin/bash结尾的行

3、显示/etc/passwd文件中ID号最大的用户的用户名

4、如果root用户存在,显示其默认的shell程序

5、显示/etc/passwd文件中两位或三位数

6、显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存在非空白字符的行

7、找出'netstat -tab'命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行

8、添加用户bash,testbash,basher以及nologin,要求nologin的shell为/sbin/nologin,而后找出/etc/passwd文件中用户名同shell名的行


1、显示/proc/meminfo文件中以大写s开头的行,要求使用两种方式

1
2
# grep -i '^s' /proc/meminfo
# grep '^[sS]' /proc/meminfo

结果

1
2
3
4
5
6
7
SwapCached:          172 kB
SwapTotal:       1049596 kB
SwapFree:        1048880 kB
Shmem:               676 kB
Slab:              30440 kB
SReclaimable:      17672 kB
SUnreclaim:        12768 kB

2、显示/etc/passwd文件中不以/bin/bash结尾的行

1
2
3
4
# grep -v '/bin/bash$' /etc/passwd
bin:x:1:1:bin: /bin : /sbin/nologin
daemon:x:2:2:daemon: /sbin : /sbin/nologin
.....

3、显示/etc/passwd文件中ID号最大的用户的用户名

1
2
3
4
5
6
7
8
# sort -n -r -t':' -k3 /etc/passwd | head -n 1
nfsnobody:x:65534:65534:Anonymous NFS User: /var/lib/nfs : /sbin/nologin
 
# awk -v FS=':' 'BEGIN{print "----------------------------------"}{if(NR==1){MIN=$3;MAX=MIN;USER=$1;MINUSER=$1}};{if(NR!=1){if($3>MAX){MAX=$3;USER=$1};if($3<MIN){MIN=$3;MINUSER=$1}}}END {printf "MAX_UID: %-10i USER: %s\nMIN_UID: %-10d MIN_USER: %s\n-----------------------------------\n",MAX,USER,MIN,MINUSER}' /etc/passwd
----------------------------------
MAX_UID: 65534      USER: nfsnobody
MIN_UID: 0          MIN_USER: root
-----------------------------------

4、如果root用户存在,显示其默认的shell程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# grep -q '^root\b' /etc/passwd && printf "root exist and default shell is " && grep '^root\>' /etc/passwd | cut -d':' -f7
root exist and default shell is  /bin/bash
 
# sed -n '/^root\>/p' /etc/passwd > /dev/null 2>&1 && echo -ne 'root exist and default shell is\t' && sed -n '/^root\>/p' /etc/passwd | awk -v FS=':' '{print $NF}'
root exist and default shell is     /bin/bash
 
# awk -F':' '$1 ~ /\<root\>/{print $NF}' /etc/passwd
/bin/bash
 
# awk -F':' '$1 == "root"{print $NF}' /etc/passwd
/bin/bash
 
# sed -n "`awk -F':' '$1 == "root" {print NR}' /etc/passwd`p" /etc/passwd 
root:x:0:0:root: /root : /bin/bash


5、显示/etc/passwd文件中两位或三位数

1
2
3
4
5
方法一:
# grep -o '\<[0-9]\{2,3\}\b' /etc/passwd
# grep -o '\<[[:digit:]]\{2,3\}\b' /etc/passwd
方法二:
# egrep -o '\<[[:digit:]]{2,3}\b' /etc/passwd

6、显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存在非空白字符的行

1
2
3
# grep '^[[:space:]]\+[^[:space:]]' rc.sysinit 
# sed -n '/^[[:space:]]\+[^[:space:]]/p' rc.sysinit
# awk '/^[[:space:]]+[^[:space:]]/ {print }' rc.sysinit

7、找出'netstat -tab'命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行

1
# netstat -tan | grep 'LISTEN[[:space:]]*'

8、添加用户bash,testbash,basher以及nologin,要求nologin的shell为/sbin/nologin,而后找出/etc/passwd文件中用户名同shell名的行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@izpo45bh60h6bsz ~] # useradd bash
[root@izpo45bh60h6bsz ~] # useradd testbash
[root@izpo45bh60h6bsz ~] # useradd basher
[root@izpo45bh60h6bsz ~] # useradd -s /sbin/nologin nologin
 
# grep '\(^[[:alnum:]]\+\>\).*\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
nologin:x:2018:2018:: /home/nologin : /sbin/nologin
 
# sed -n '/\(^[[:alnum:]]\+\>\).*\1$/p' /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
nologin:x:2018:2018:: /home/nologin : /sbin/nologin
 
# awk -F':' '{split($NF,A,"/")}{if (A[3] == $1){print}}' /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
nologin:x:2018:2018:: /home/nologin : /sbin/nologin










本文转自 lccnx 51CTO博客,原文链接:http://blog.51cto.com/sonlich/1952534,如需转载请自行联系原作者
目录
相关文章
|
11月前
|
Unix Linux Perl
我们一起来学grep
我们一起来学grep
71 0
|
NoSQL Java Linux
netstat -anp | grep 应用 讲解 详细
netstat -anp | grep 应用 讲解 详细
441 0
netstat -anp | grep 应用 讲解 详细
|
Unix Perl
三剑客之 grep
三剑客之 grep
|
Web App开发 应用服务中间件 PHP