2016年8月6日作业练习

练习:

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

[root@localhost ~]# grep -i "^[sS]" /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2098172 kB
SwapFree:        2098172 kB
Shmem:              6916 kB
Slab:             179376 kB
SReclaimable:     138252 kB
SUnreclaim:        41124 kB

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

grep -v "/bin/bash$" /etc/passwd 
    #-v 显示除了匹配到的行
    #"/bin/bash$ 将$前面的字符串锚定到行尾

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

[root@localhost ~]# grep  "^rpc" /etc/passwd
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
#显示过滤到的结果不正确。用户rpc,rpc是一个单词

#应该锚定单词
[root@localhost ~]# grep  "^\<rpc\>" /etc/passwd | cut -d: -f1,7
rpc:/sbin/nologin

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

grep "[[:digit:]][[:digit:]][[:digit:]]\?" /etc/passwd
以下为部分内容
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

其中nfsnobody:x:65534:65534:##也被匹配到了
匹配到的原因是655、553、534.都是符合条件的。所以整个66534就都被匹配到了。
应该改为锚定单词,将两位数或三位数当成一个单词来处理。

应该如下写法
grep "\<[[:digit:]][[:digit:]][[:digit:]]\?\>" /etc/passwd

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

[root@localhost ~]# grep "^[[:space:]]\+.*[^[:space:]]$" /etc/grub2.cfg

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

[root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
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     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN

7、添加用户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:1001:1001::/home/bash:/bin/bash
nologin:x:1004:1004::/home/nologin:/sbin/nologin

练习-用扩展正则表达式

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

[root@localhost ~]# egrep "^root|^wang|^mage" /etc/passwd
root:x:0:0:root:/root:/bin/bash
mageedu:x:1000:1000:mageedu:/home/mageedu:/bin/bash
wang:x:1005:1005::/home/wang:/bin/bash

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

[root@localhost ~]# grep "^[^([:space:]].*(" /etc/rc.d/init.d/functions 
[root@localhost ~]# egrep "^[^\([:space:]].*\(" /etc/rc.d/init.d/functions

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

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

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

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

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

[root@localhost ~]# last | egrep "[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*" | tr -s " " | sort -t" " -k3 -n |cut -d " " -f3 | uniq -c
  5 192.168.226.1

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

[0-9]
[1-9]?[0-9]
2[0-4]?[0-9]
25[0-5]

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

常用ip地址是ABC类,地址范围1.0.0.0-223.255.255.255
[root@localhost ~]# ifconfig |tr -s " " |egrep -o "^.*inet[[:space:]][[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*"| cut -d" " -f3
192.168.226.129
127.0.0.1

课后作业:

1、取本机ip地址

[root@localhost ~]# ifconfig |tr -s " " |egrep "broadcast" | cut -d" " -f3

192.168.226.129

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

[root@localhost ~]# df | grep "^.*sda.*[[:digit:]]\+%"| tr -s " " |cut -d" " -f5,6
1% /
13% /usr
29% /boot

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

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

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

[root@localhost ~]# vim lujing.sh 
#!/bin/bash
#取路径中除基名以外的路径

echo "/etc/rc.d/functions"                           #显示完整路径
fullpath="/etc/rc.d/functions"                       #定义完整路径变量
bcnum=`echo $fullpath |egrep  -o "[^/]+/?$" | wc -c` #计算基名字数
fullnum=`echo $fullpath | wc -c`                     #计算完整路径字数
pathcount="$[$fullnum-$bcnum]"                      #计算后的路径字数也可以用bc计算
echo $fullpath|cut -c1-$pathcount                  #cut出路径

[root@localhost ~]# bash lujing.sh 
/etc/rc.d/functions
/etc/rc.d/
[root@localhost ~]#

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

(0)
yywyyw
上一篇 2016-08-07 22:06
下一篇 2016-08-07 22:06

相关推荐

  • 第六周作业

    1、简述osi七层模型和TCP/IP五层模型 应用层 运行着应用程序的协议http、ftp、tftp等 表示层 将应用层所编辑的数据进行加密或解密,没有协议 会话层 决定一次数据传输发起和接收会话请求,没有协议 传输层 根据选着的协议类型,决定是否将数据分割或重组,协议:tcp、udp等 网络层 编辑逻辑地址信息(原站点、目的站点)和路由选着(ARP)协议:…

    2018-02-06
  • 随笔

    GREP正则表达式: 复习: glob文件通配符:     *:任意长度字符:     ?:任意单个字符:     []:括号内的任意单个字符:     [^]:括号内字符除外:  &nbs…

    Linux干货 2016-07-16
  • N25-第一周博客作业

    1、描述计算机的组成及其功能:    没有听马哥视频之前,计算机的组成理解为主机+显示器;    听完视频之后,对计算机的组成分为硬件系统和软件系统。    硬件系统主要有:运算器、控制器、存储器、输入设备和输出设备;    软件系统主要有…

    Linux干货 2016-12-04
  • M20-1 8月3号作业

    1、三种权限rwx对文件和目录的不同意义 2、umask和acl mask 的区别和联系 3、三种特殊权限的应用场景和作用 4、设置user1,使之新建文件权限为rw——- 5、设置/testdir/f1的权限,使user1用户不可以读写执行,g1组可以读写 /testdir/dir的权限,使新建文件自动具有acl权限:user1:r…

    Linux干货 2016-08-05
  • vim文本操作、例行性工作、脚本编程_第六周练习(01)

    vim文本操作 Q1:复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@promote /]# cp /etc/rc.d/rc.sysinit /tmp/ [root@promote /]# l…

    Linux干货 2016-12-18
  • LINUX HASH命令

    LINUX:Hash命令 介绍:linux系统下会有一个hash表,刚开始这个hash表为空,每执行过一条命令hash表会缓存下这条命令。Shel优先会查看hash表。 hash缓存表可大大提高命令的调用速率 下面介绍几个hash常用选项 刚进入系统使用Hash命令 哈希表为空 使用过命令后使用Hash命令 使用过命令后,Hash可显示缓存的命令 Hash …

    Linux干货 2017-04-03