8月5号 练习+作业

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

[root@localhost ~]# ifconfig |tr -cs '[:digit:].' '\n' |sort -t. -k3 |tail -5
255.0.0.0
255.255.0.0
127.0.0.1
10.10.10.10
10.10.255.255

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

[root@localhost ~]# df |tr -s ' ' % |cut -d% -f5 |sort -n |tail -1
21

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

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

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

[root@localhost ~]# stat /tmp |head -4 |tail -1|tr -s '(/' '::' |cut -d: -f3
1777

[root@localhost ~]# stat /tmp |head -4 |tail -1 |tr -cs '[0-9]' '\n' |head -2 |tail -1
1777

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

[root@localhost ~]# netstat -nt |tr -s ' ' ':' |cut -d: -f6 |tr -d '[:alpha:]' |sort -r |uniq -c
      2 10.10.10.1
      2

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

[root@localhost ~]# grep -i '^s' /proc/meminfo
[root@localhost ~]# grep '^[sS]' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:               240 kB
Slab:              58904 kB
SReclaimable:      32592 kB
SUnreclaim:        26312 kB

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

[root@localhost ~]# grep -v '/bin/bash$' /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
sync:x:5:0:sync:/sbin:/bin/sync

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

[root@localhost ~]# grep '^rpc\>' /etc/passwd |cut -d: -f7
/sbin/nologin

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

[root@localhost ~]# grep '\<[0-9]\{2,3\}' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin

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

[root@localhost ~]# grep '^[[:space:]]\+[^[:space:]]*' /etc/grub2.cfg
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"

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

[root@localhost ~]# netstat -ant |grep 'LISTEN[[:space:]]*$'
tcp        0      0 0.0.0.0:57160               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN

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

[root@localhost ~]# grep '^\([^:]\+\>\).*/\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
bash:x:507:514::/home/bash:/bin/bash
nologin:x:510:517::/home/nologin:/sbin/nologin

13、显示三个用户root mage wang UID 和默认shell

[root@localhost ~]# grep -E '^(root|mage|wang)\>' /etc/passwd |cut -d: -f1,3,7
root:0:/bin/bash
mage:511:/bin/bash
wang:512:/bin/bash

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

[root@localhost ~]# grep -E '^[_[:alpha:]]+\(\)' /etc/rc.d/init.d/functions 
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {

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

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

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

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

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

[root@localhost ~]# last |grep '^root\>' |tr -s ' ' |cut -d' ' -f3 |grep '^[[:digit:]]' |sort |uniq -c
     20 10.10.10.1
      2 10.1.69.10
      1 10.1.69.200
      1 10.1.69.70

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

0-9:     [0-9]
10-99:    [1-9][0-9]
100-199:  1[0-9][0-9]
200-249:  2[0-4][0-9]
250-255:  25[0-5]

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

[root@localhost ~]# ifconfig |grep '\<inet\>' |tr -s ' ' ':' |cut -d: -f4
10.10.10.10
127.0.0.1

20、取本机ip地址

[root@localhost ~]# ifconfig |grep '\<inet\>' |tr -s ' ' ':' |cut -d: -f4
10.10.10.10
10.1.253.35
127.0.0.1

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

[root@localhost ~]# df |grep '^/dev/sd' |tr -s ' ' ':' |cut -d: -f1,5 |tr -d %
/dev/sda2:2
/dev/sda5:1
/dev/sda1:53

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

[root@localhost ~]# cat /etc/init.d/functions |tr -cs '[:alpha:]' '\n' |sort |uniq -c | sort -n -r
     67 pid
     55 if
     54 file
     51 echo
     47 then
     45 return
     45 fi
     33 n

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

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

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

[1-9][0-9]{5}:前6位数字

(19[0-9][0-9]|200[0-9]|201[0-6]:4位代表年份

(0[0-9]|1[0-2]):2位代表月份

([0-2][0-9]|3[0-1]:2位代表日期

[0-9]{3}([0-9]|X):后4位随机数(包含出现尾数为X的情况)

[root@localhost ~]# echo "ID:44162219810327471X" |egrep -o '\<[1-9][0-9]{5}(19[0-9][0-9]|200[0-9]|201[0-6])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])[0-9]{3}([0-9]|X)\>'
44162219810327471X
[root@localhost ~]# echo "ID:441622198103274710" |egrep -o '\<[1-9][0-9]{5}(19[0-9][0-9]|200[0-9]|201[0-6])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])[0-9]{3}([0-9]|X)\>'
441622198103274710

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

[root@localhost ~]# echo "phone number 18925619340" |egrep -o "\<1[3|5|7|8][0-9]{9}\>"
18925619340

26、正则表达式表示邮箱

[root@localhost ~]# cat mail |egrep '^[[:alnum:]]+@[[:alnum:]]+.com\>'
wangwu@163.com
123456@qq.com
li23si@sohu.com

27、正则表达式表示QQ号

  QQ号5-10位:

[1-9]:第一个数字不能为0
[0-9]{4,9}:后面跟4-9位数字
[root@localhost ~]# echo "My QQ is 9087654321" |egrep -o "\<[1-9][0-9]{4,9}\>"
9087654321
[root@localhost ~]# echo "My QQ is 54321" |egrep -o "\<[1-9][0-9]{4,9}\>"
54321

 


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

(0)
pingskypingsky
上一篇 2016-08-07 22:05
下一篇 2016-08-07 22:05

相关推荐

  • LVM原理、创建、扩容、缩减、快照详解

    LVM是什么?为什么要使用LVM?     LVM(Logical Volume Manager):逻辑卷管理, 在日常使用或生产环境中, 我们可能会因为在规划存储时未考虑到未来数据增长的速度超乎我们的预计而措手不及,因为增加一块硬盘再将源数据移到新硬盘上很麻烦并且提高了成本还浪费硬盘空间。   &…

    Linux干货 2016-03-09
  • N26-博客作业-week9

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; #!/bin/bash # declare -i nologin_num=0,login_num=0 for i in $(cut -d: -f7 /etc/passwd); do if …

    Linux干货 2017-04-01
  • 文本处理工具-习题

    1 、找出ifconfig 命令结果中本机的所有IPv4地址 [root@centos7 ~]# ifconfig |head -2 |tail-1 |cut -dn -f2 |cut -d" " -f2 2 、查出分区空间使用率的最大百分比值 [root@centos7 ~]# df |cut -c44-46 |sort -n|tail…

    Linux干货 2016-08-15
  • linux常用网络配置

    网卡名称:  网络接口识别并命名相关的udev配置文件:    /etc/udev/rules.d/70-persistent-net.rules  卸载网卡驱动:    modprobe  -r e1000  装载网卡驱动:    modprobe &nbs…

    Linux干货 2016-09-06
  • Linux系统上获取命令的帮助信息方法

    当我们在操作linux时忘记相关命令的用法时,可以使用man 命令或者命令 -h来查询该命令的用法; man文档共有9个章节 1:所有用户可以操作的指令或可执行文件 2:系统核心调用的函数与工具 3:子调用,常用的函数与函数库 4:设备,硬件文件说明,通常是/dev/的文件 5:文件格式,配置文件或者是某些档案的格式 6:游戏相关 7:杂项,例如linux文…

    Linux干货 2018-03-04
  • 第5周

    1, ~]# grep "^root\>" /etc/passwd | cut -d: -f7 4,~]# ifconfig | grep "\<[0-9][0-9][0-9]\>" 7, ~]# find /var -user root -group mail 8,~]# fin…

    Linux干货 2016-09-15