第三周(3):课堂练习与作业

课堂练习:

1、找出ifconfig命令结果中本机的所有IPv4地址

[root@centos6 ~]# ifconfig | tr -cs '[0-9].' '\n'|sort -ut. -k3n

127.0.0.1
10.1.255.255
10.1.255.76

2、查出分区空间使用率的最大百分比值

[root@centos6 ~]# df | tr -s ' ' ':' |cut -d : -f5|tr -d % |sort -rn |head -n1
22

3、查出用户UID最大值的用户名、UID及shell类型

[root@centos6 ~]# cat /etc/passwd |sort -t: -k3 -n|cut -d: -f1,3,7 |tail -1
nfsnobody:65534:/sbin/nologin

4、查出/tmp的权限,以数字方式显示

[root@centos6 ~]# stat /tmp | cut -d: -f2|head -4|tail -1|tr -cd '0-9\n'
1777

5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@centos6 ~]# netstat -nt | tr -cs '[0-9].' '\n'|sort -ut. -k3nr
10.1.255.76
10.1.250.48

6、显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式)

[root@centos6 ~]# grep '^[S|s]' /proc/meminfo
[root@centos6 ~]# grep -i '^s' /proc/meminfo

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

[root@centos6 ~]# grep -v "/bin/bash$" /etc/passwd

8、显示用户rpc默认的shell程序

[root@centos6 ~]# grep "^rpc\b" /etc/passwd | cut -d: -f7

9、找出/etc/passwd中的两位或三位数

grep -o "[[:digit:]]\{2,3\}" /etc/passwd

10、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行

grep '^[[:space:]]\+[^[:space:]]' /etc/grub.conf   #centos6已"grub.conf"文件为参考

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

[root@centos6 ~]# netstat -tan |grep 'LISTEN[[:space:]]*$'

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

[root@centos6 ~]# useradd bash
[root@centos6 ~]# useradd testbash
[root@centos6 ~]# useradd basher
[root@centos6 ~]# useradd -s /sbin/nologin nologin
[root@centos6 ~]# grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd

13、显示当前系统root、mage或wang用户的UID和默认shell

[root@centos6 ~]# egrep '^(root|mage|wang)\b' /etc/passwd

14、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[root@centos6 ~]# egrep '^[[:alpha:]_]+\(\)' /etc/rc.d/init.d/functions

15、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@centos6 ~]# echo "/etc/rc.d/init.d/functions" | egrep -o "[^/]+/?$"

16、使用egrep取出上面路径的目录名

[root@centos6 ~]# echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/+\<"

17、统计以root身份登录的每个远程主机IP地址的登录次数

[root@centos6 ~]# last | grep -E -o '^root\>.*([[:digit:]]\.){3}[[:digit:]]' | tr -s '' |cut -d ' ' -f3 |sort|uniq -c

18、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

[root@centos6 ~]# seq 0 255 |egrep '[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]' | tr -cs '0-9' '\t'

19、显示ifconfig命令结果中所有IPv4地址  

ifconfig |grep -E -o  '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'

作业:

1、取本机ip地址

ifconfig |grep -E -o  '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' |head -1

2、取各分区利用率的数值

[root@centos6 ~]# df | tr -s ' ' '|' |cut -d '|' -f5

3、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

[root@centos6 ~]# cat /etc/init.d/functions | tr -cs '[:alpha:]' '\n' | sort | uniq -c | sort -nr

4、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/"  取目录名

echo "/etc/rc.d/init.d/functions" | egrep -o '^/.*/\<'

5、正则表达式表示身份证号(以第二代身份证为参考)

blob.png

grep -E -o -i '[1-6][0-7][0-9]{2}[0-4][0-9]((1[89][0-9]{2})|(200[0-9]|201[0-5]))(((0[13578]|1[02])([0-2][0-9]|3[01]))|((0[469]|1[02])([0-2][0-9]|30))|02[012][0-9])[0-9]{3}[0-9x]'

6、正则表达式表示手机号

blob.png

[root@centos6 ~]# grep -E -o '1(3|4|5|7|8|9)[0-9]{9}'

7、正则表达式表示邮箱(已163邮箱为例)

blob.png

[root@centos6 ~]# grep -E -o '[[:alnum:]]([[:alnum:]]|[[:punct:]]){5,17}@163\.com'

8、正则表达式表示QQ号(数字登陆)

[root@centos6 ~]# grep -E -o '[1-9][0-9]{4,10}'

原创文章,作者:Aleen,如若转载,请注明出处:http://www.178linux.com/30621

(0)
AleenAleen
上一篇 2016-08-08
下一篇 2016-08-08

相关推荐

  • 马哥面授班20期-第一周(计算机基础,linux入门)

    一.计算机发展历史及硬件组成部分     计算机发展历史:第一代:(1946-1957)电子管时代,第二代:(1958-1964)晶体管时代,              第三代:(1965-1970)集成电路时…

    Linux干货 2016-07-26
  • class7 文本处理命令及文本处理工具grep

    一、文本处理命令   1、文件内容查看cat, tac,rev     cat [OPTION]… [FILE]…      正序查看文本文件          -E:  显示行结束符$ [roo…

    Linux干货 2016-08-08
  • 8.1_Linux习题和作业

    7.28 作业 1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中 1 # cat /etc/issue | tr 'a-z' 'A-Z'whoi > /tmp/issue.out 2、将当前系…

    Linux干货 2016-08-04
  • linux下小练习(2)

    1、编写脚本/bin/per.sh,判断当前用户对指定的参数文件,  是否不可读并且不可写                         2、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充 许…

    Linux干货 2016-08-15
  • 运维挖坑埋坑之旅—-413 Request Entity Too Large

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1575160        好久没写博文了,天天忙的喘不过来气。最近是该总结总结前段时间的工作了,顺便缓解下这两天的霉…

    Linux干货 2016-08-15
  • 在Centos7上源码编译httpd

    在Centos7上源码编译httpd 安装源码包 首先在http://httpd.apache.org下载所需要的安装包 小编在这里下载的是httpd-2.4.27.tar apr-1.6.2.tar apr-util-1.6.0.tar 首先编译安装apr-1.6.2.tar.gz #1 tar -zxf apr-1.6.2.tar.gz #先解压安装包 …

    2017-08-21