面授20-1班 0805课间练习与课后作业

课间练习


第一阶段

新学的命令文本命令cat tac rev more less head tail cut paste wc sort dif patch

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

[root@IP70-CentOS7 ~]# >>ifconfig | tr -cs '0-9.' '\n' | sort -t . -k3 | tail -8

1470401824465311.png

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

[root@IP70-CentOS7 ~]# >>df | tr -s ' ' | cut -d' ' -f5 | tr -dc '0-9\n' | sort -n | tail -1

1470402179214486.png

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

[root@IP70-CentOS7 ~]# >>sort -t: -k3n /etc/passwd |tail -1 | cut -d: -f1,3,7

1470402598686145.png

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

stat /tmp | head -4 | tail -1 | tr -cs '0-9' '\n' | tail -3 | head -1

1470402788521387.png

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

[root@IP70-CentOS7 ~]# >>netstat -nt | tr -s ' ' ':' | cut -d: -f6 | tr -d '[:alpha:]' | uniq -c | sort -r

1470403837135168.png

第二阶段

grep 正则表达式

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

[root@IP70-CentOS7 ~]# >>grep -e ^s -e ^S /proc/meminfo
[root@IP70-CentOS7 ~]# >>grep -i ^s /proc/meminfo
[root@IP70-CentOS7 ~]# >>grep -i ^[sS] /proc/meminfo

1470404355822702.png

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

[root@IP70-CentOS7 ~]# >>grep -v '/bin/bash$' /etc/passwd

1470404477267048.png

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

[root@IP70-CentOS7 ~]# >>grep '^rpc\b' /etc/passwd | cut -d: -f7

1470439923490056.png

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

[root@IP70-CentOS7 ~]# >>egrep -o '\b[0-9]{2,3}\b' /etc/passwd

1470440091416289.png

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

[root@IP70-CentOS7 ~]# >>egrep '^[[:space:]]+[^[:space:]]+$' /etc/grub2.cfg

1470440614495907.png

6、 、 找出“netstat -tan” 命令 的结果 中以 以‘LISTEN’ 后跟任意多个空白字符结尾的行

[root@IP70-CentOS7 ~]# >>netstat -tan | egrep '\bLISTEN[[:space:]]*$'

1470440968501105.png

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

[root@IP70-CentOS7 ~]# >>useradd bash
[root@IP70-CentOS7 ~]# >>useradd testbash
[root@IP70-CentOS7 ~]# >>useradd basher
[root@IP70-CentOS7 ~]# >>useradd nologin -s /sbin/nologin
[root@IP70-CentOS7 ~]# >>egrep '^([^:]*):.*/\1$' /etc/passwd

1470442631965909.png

 

课后作业


 

4、取本机ip地址

思考:本机ip地址包括ipv4和ipv6,在ifconfig输出显示是跟着inet和inet6后面字段,还要滤掉127.0.0.1和::1的两个回环地址

[root@IP70-CentOS7 ~]# >>ifconfig | egrep 'inet6?\b' | tr -s ' ' | cut -d' ' -f3 | fgrep -v -e127.0.0 -e ::1

1470450808666102.png

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

思考:关键字段/dev/sd过滤df输出结果,然后替换重复空格,直接用cut可以提取

[root@IP70-CentOS7 ~]# >>df | grep '/dev/sd' | tr -s ' ' | cut -d' ' -f5 | tr -d %

Image 20160806090805.png

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

思考:tr滤掉所有非字母的字符,sort排序后进行uniq词频统计,再次sort从高到低排序

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

1470446235435204.png

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

方法一:先用grep匹配去掉最后的/,再将输出进行二次匹配,去掉最后的单词。

[root@IP70-CentOS7 ~]# >>\cat testdir | grep -o '^/.*[^/]' | grep -o '^/.*/'

1470542342468329.png

方法二:文件的基名是个单词,/etc/rc.d/init.d/functions/,按位置排列/为12345

    4号5号的区别在于:4号处于单词(基名)的前面,而5号后面是没有单词的(即不在某单词的前面),所以我们可以利用单词锚定,确定匹配/:

[root@IP70-CentOS7 tmp]# >>\cat testdir | grep -o '^.*/\b'
[root@IP70-CentOS7 tmp]# >>\cat testdir | grep -o '^.*/\<'

1470552329617510.png

8、正则表达式表示身份证号

思考: 身份证号为18位,

    前6位地区编号(非0开头):

        [1-9][0-9]{5}

    后8位出生年月日

        本例取1800年-2015年

[root@IP70-CentOS7 ~]# >>\cat year | egrep -w '(1[89][0-9]{2})|(200[0-9]|201[0-5])'

            1470470835674775.png

        月份01-12,日期01-31,其中01、03、05、07、08、10、12月有31天,04、06、09、11月30天,02月28天

[root@IP70-CentOS7 ~]# >>\cat mmdd | egrep '((0[13578]|1[02])([0-2][0-9]|3[01]))|((0[469]|1[02])([0-2][0-9]|30]))|02[012][0-9]'

            1470472046215143.png          

    后四位末位有数字或X。

[root@IP70-CentOS7 ~]# >>\cat mmdd | egrep -w '[0-9]{3}[0-9x]'

            1470472451553720.png        

综合上述:

[root@IP70-CentOS7 ~]# >>\cat file | egrep -w '[1-9][0-9]{5}((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]'

1470481899738861.png

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

思考:手机号码11位数字,网上搜索中国手机号起始3位如下,

中国移动
GSM 134 135 136 137 138 139 150 151 152 157 158 159
3G(TD-SCDMA) 187 188
中国联通
GSM 130 131 132 155 156
3G(WCDMA) 185 186
中国电信
CDMA 133 153
3G(CDMA2000) 180 189

即有13[0-9]、15[0-35-9]、18[05-9],前三位确定,再有8位任意数字就可以了(注:本例只适用于中国地区以上列举手机号段的匹配)

[root@IP70-CentOS7 ~]# >>\cat tel1 | egrep -w '(86)?((13[0-9])|(15[0-35-9])|(18[05-9]))[0-9]{8}'

1470454834820699.png

10、正则表达式表示邮箱

思考:邮箱格式为 用户名@网站名.网站后缀

用户名:

纯字母

纯数字

数字、字母(可以数字开头或字母开头)

数字、字母、“_”或“-”(符号"_"和"-"不能做词首词尾)

即:

    \b[[:alnum:]]+[0-9a-zA-Z-_]*[[:alnum:]]

网站:

数字字母.字母

[[:alnum:]]+\.[[:alpha:]]+

[root@IP70-CentOS7 ~]# >>\cat tel1 | egrep '^[[:alnum:]]+[-_[:alnum:]]*[[:alnum:]]\@[[:alnum:]]+\.[[:alpha:]]+'

1470466065711418.png

11、正则表达式表示QQ号

思考:现在 的QQ是5-11位不以0开始的纯数字

[root@IP70-CentOS7 ~]# >>\cat testqq | egrep -w '[1-9][0-9]{4,10}'

Image 20160806153600.png

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

(0)
昭其昭其
上一篇 2016-08-07
下一篇 2016-08-07

相关推荐

  • awk中pipe的shell命令调用与close()

        某公司其员工到勤时间表如下, 取名为 arr.dat, 文件中第一栏为员工代号, 第二栏为到达时间. 本示例中将使用该文件为数据示例文件, 1034 7:26 1025 7:27 1101 7:32 1006 7:45 1012 7:46 1028 7:49 1051 7:51 1029 7:57 1042 7…

    Linux干货 2016-11-11
  • 系统启动流程

    linux系统启动流程 内核的设计结构单内核:linux(线程–lwp轻量级进程)微内核:windows(支持真正意义上的多线程) 单内核:很多功能驱动都集成在一起 微内核:内核很小,功能单一。模块化 linux为了适应众多用户的不同硬件需求,linux内核在设计上采用模块化设计。可以动态加载模块。核心模块:ko 内核所独有的。共享对象:so 红…

    Linux干货 2016-09-19
  • Beep Beep Casino Bonusangebote und Aktionen

    Im Beep Beep Casino freue ich mich über die vielen Boni, die mein Spielvergnügen deutlich verbessern. Von Willkommensboni mit Einzahlungsboni und Freispielen bis hin zu Reload-Boni…

    Linux干货 2025-05-15
  • rpm管理包

    rpm命令是RPM软件包的管理工具。rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎。逐渐受到其他发行版的采用。RPM套件管理方式的出现,让Linux易于安装,升级,间接提升了Linux的适用度。接下来聊一聊如何安装、卸载、查询、检验rpm类的软件。 安装:…

    2017-08-19
  • 马哥教育网络班21期+第7周课程练习

    1、创建一个10G分区,并格式为ext4文件系统;     fdisk /dev/sdb    n p 1 +10G w (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl…

    Linux干货 2016-08-22
  • 正则表达式

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户都没有任何访问权限 [root@localhost ~]#  mkdir /home/tuser1======>创建/home/tuser1目录 [root@localhost ~]#   cp  -a  /etc/skel/   /hom…

    2017-10-10