linux grep命令及实例,linux常用命令练习:grep具体使用实例

grep常用练习:

1、显示/proc/meminfo文件中以不区分大小的s开头的行;

grep -i '^s' /proc/meminfo

[root@xuelinux ~]# grep -i '^s' /proc/meminfo

SwapCached:            0 kB

SwapTotal:       2047992 kB

SwapFree:        2047992 kB

Shmem:               252 kB

Slab:              48992 kB

SReclaimable:      17484 kB

SUnreclaim:        31508 kB

2、显示/etc/passwd中以nologin结尾的行;

grep 'nologin$' /etc/passwd

[root@xuelinux ~]# grep '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

取出默认shell为/sbin/nologin的用户列表

grep '\/sbin\/nologin$' /etc/passwd | cut -d: -f1

[root@xuelinux ~]# grep '\/sbin\/nologin$' /etc/passwd | cut -d: -f1

bin

daemon

adm

lp

mail

取出默认shell为bash,且其用户ID号最小的用户的用户名

grep 'bash$' /etc/passwd | sort -n -t: -k3 | head -1 | cut -d: -f1

[root@xuelinux ~]# grep 'bash$' /etc/passwd | sort -n -t: -k3 | head -1 | cut -d: -f1

root      先查找以bash结尾,然后通过sort 按以:为分隔符(-t:)的第三字段(-k3)用户ID号数值从小到大排序-n然后取第一行head -1 最后剪切第一列用户名。

3、显示/etc/inittab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行;

grep '^#[[:space:]]\{1,\}[^[:space:]]' /etc/inittab

[root@xuelinux ~]# grep '^#[[:space:]]\{1,\}[^[:space:]]' /etc/inittab

#   0 - halt (Do NOT set initdefault to this)

#   1 - Single user mode

#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)

#   3 - Full multiuser mode

#   4 - unused

#   5 - X11

#   6 - reboot (Do NOT set initdefault to this)

4、显示/etc/inittab中包含了:一个数字:(即两个冒号中间一个数字)的行;

grep ':[[:digit:]]:' /etc/inittab

[root@xuelinux ~]#  grep ':[[:digit:]]:' /etc/inittab

id:3:initdefault:

5、显示/boot/grub/grub.conf文件中以一个或多个空白字符开头的行;

grep '^[[:space:]]\{1,\}' /boot/grub/grub.conf

[root@xuelinux ~]# grep '^[[:space:]]\{1,\}' /boot/grub/grub.conf

root (hd0,0)           此行有多个空白字符开头

kernel /vmlinuz-2.6.32-431.el6.i686 ro root=UUID=65a8762e-b49a-47a6-8a4b-4368275b1f7a rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet  此处为整体一行,多个空白字符开头

initrd /initramfs-2.6.32-431.el6.i686.img   此行有多个空白字符开头

6、显示/test/test1.txt文件中以一个数字开头并以一个与开头数字相同的数字结尾的行;

grep '^\([[:digit:]]\).*\1$' /test/test1.txt

[root@xuelinux test]# grep '\([[:space:]]\).*\1$' /test/test1.txt

2who are you 2

4 youareringt4

5,he is 2tom2

7 eg is over 11

8 thanks you 1and1

[root@xuelinux test]# cat test1.txt

1,this is helloword!  此处没有以数字结尾

2who are you 2   此处匹配到

3,no 4 is not 2.  此处结尾是以.结尾所以未匹配到

4 youareringt4.   此处结尾是以.结尾所以未匹配到

4 youareringt4   此处匹配到,是以前面出现的数字4结尾

5,he is 2tom2   此处匹配到,是以前面出现的数字2结尾

5,he is 2tom2.   此处结尾是以.结尾所以未匹配到

6 tom is cat4,yes is 4. 此处结尾是以.结尾所以未匹配到

7 eg is over 11.  此处结尾是以.结尾所以未匹配到

7 eg is over 11  此处匹配到,是以前面出现的数字1结尾

8 thanks you 1and1  此处匹配到,是以前面出现的数字1结尾

7、找出ifconfig命令结果中的1-255之间的整数;

ifconfig |  grep -E  '\'

8、查找当前系统上名字为student(必须出现在行首)的用户的帐号的相关信息;

grep '^student\>' /etc/passwd | cut -d: -f1

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值