文本处理工具练习题(包含正则)

正则练习题(包含文本处理练习题)

问题

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

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

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

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

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

答;

  1. ifconfig | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'

  2. df -h | tr -s ' ' ':' | cut -d : -f 5 | cut -d '%' -f1

  3. cat /etc/passwd | sort -t : -k3n | cut -d : -f1,3,6 | tail -n 1

  4. stat /tmp/ | tr -s ' ' ':' | cut -d : -f 2 | grep ^'(' | grep -o [0-9][0-9][0-9][0-9]或者 stat /tmp |head -4|tail -1 |tr "/" "("|cut -d "(" -f2

  5.  netstat -nt | grep  tcp | tr -s " " ";" | cut -d ";" -f5 | uniq -c | sort -n

问题

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

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

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

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

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

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

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

答:

  1. cat /proc/meminfo | grep -i ^s或者 grep ^[sS]

  2. cat /etc/passwd | grep -v "/bin/bash"

  3. cat /etc/passwd | grep -w rpc | cut -d : -f 7

  4. cat /etc/passwd | grep -n '[0-9]\{2,3\}'

  5. cat /etc/grub2.cfg | grep "^[[:space:]]\{1,\}.\{1,\}"

  6. netstat -tan | grep 'LISTEN[[:space:]]*$'

  7. grep -n '^\(\b[[:alnum:]]\{1,\}\b\):.*\1$' /etc/passwd  或者grep -n '^\(\b.*\{1,\}\b\):.*\1$' /etc/passwd

问题

  1. 显示三个用户root、mage、wang的UID和默认shell

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

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

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

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

  6. 显示ifconfig命令结果中所有IPv4地址

答:

  1. cat /etc/passwd | egrep '^\b(root|user1|user2)\b' | cut -d : -f 1,3,7

  2. cat /etc/rc.d/init.d/functions | grep -n -w "^.*()" 或者 egrep -n '^(\b(\w{1,})\b)\(\)' /etc/rc.d/init.d/functions

  3. echo "/etc/rc.d/init.d/functions" | egrep -o "[^/]+/?$"

  4.  echo "/etc/rc.d/init.d/functions" | egrep -o '^(/)\b.*\1\b'

  5. egrep [0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

  6. ifconfig | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

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

(0)
forestforest
上一篇 2016-08-08 16:04
下一篇 2016-08-08 16:04

相关推荐

  • 程序包管理

    程序包 linux的程序包主要分为两类;二进制可执行安装包和源代码程序文件包     二进制应用程序的组成部分:     二进制文件、库文件、配置文件、帮助文件   查看二进制程序所依赖的库文件:     ldd&nbs…

    Linux干货 2016-05-30
  • N25-第四周博客作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost tuser1]# cp -r /etc/skel/ /home/tuser1 [root@localhost tuser1]#&nb…

    Linux干货 2016-12-25
  • 路径操作&StringIO/BytesIO

    Edit 路径操作&StringIO/BytesIO 路径操作 路径操作模块: 3.4版本以前os.path模块 In [1]: from os import path In [2]: p = path.join(‘/etc’,’sysconfig’,’network’)#将字符…

    Linux干货 2017-10-30
  • 处理文本的工具sed,vim 编辑器的使用 Linux版

    处理文本的工具sed Stream EDitor 行编辑器 sed一次处理一行的内容,处理时,将当前处理的行存储在临时缓冲区,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,将缓冲区的内容送往屏幕,接着处理下一行不断重复,直到末尾。            一次处理一行 文件&#82…

    Linux干货 2016-08-11
  • 第四周作业

    作业 1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@cloud ~]# cp -R /etc/skel /home/tuser1 && chmod -R…

    Linux干货 2016-12-25
  • MySQL应用管理

    这篇文章主要讲述基本的SQL语句,以供新手参考使用,不过最好的办法还是查阅官方文档和help命令。 进入正题 什么是SQL?        SQL结构化查询语言,是关系型数据库查询和管理语言,是一种数据库查询和程序设计语言,用于存取数据以及查询,更新和管理关系型数据库系统。 SQL的分类 &nbsp…

    Linux干货 2015-09-23