文本处理课后小练习

1、找出ifconfig命令结果中本机的所有IPv4地址
[root@wzc date]# 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])'
10.1.253.22
255.255.0.0
10.1.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
2、查出分区空间使用率的最大百分比值
[root@wzc date]# df | tr -s ' '|cut -d' ' -f5|sort -n|tail -1|tr -d %
51
3、查出用户UID最大值的用户名、 UID及shell类型
[root@wzc date]# getent passwd |cut -d: -f1,3,7|sort -rnt: -k 2|head -n1
nfsnobody:65534:/sbin/nologin
4、查出/tmp的权限,以数字方式显示
[root@wzc date]# stat /tmp/|head -n4|tail -n1|cut -d/ -f1|cut -d\( -f2
1777
5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[root@wzc date]# netstat -tn |cut -d: -f2|tr -s ' ' ':' |cut -d: -f2| sort |uniq -c|head -n1
      1 10.1.250.38
6、显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式)
[root@wzc date]# grep -E '^(S|s)' /proc/meminfo ;grep '^[sS]' /proc/meminfo
SwapCached:          100 kB
SwapTotal:       3905532 kB
SwapFree:        3905116 kB
Shmem:             10160 kB
Slab:             140636 kB
SReclaimable:      79632 kB
SUnreclaim:        61004 kB
SwapCached:          100 kB
SwapTotal:       3905532 kB
SwapFree:        3905116 kB
Shmem:             10160 kB
Slab:             140636 kB
SReclaimable:      79632 kB
SUnreclaim:        61004 kB
7、显示/etc/passwd文件中不以/bin/bash结尾的行
[root@wzc date]# 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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8、显示用户rpc默认的shell程序
[root@wzc date]# getent passwd |grep '^rpc\b'|cut -d: -f7
/sbin/nologin
9、找出/etc/passwd中的两位或三位数
[root@wzc date]# grep '\<[[:digit:]]\{2,3\}\>' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
10、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行
[root@wzc date]# 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"
  menuentry_id_option=""
  set saved_entry="${prev_saved_entry}"
11、找出"netstat -tan"命令的结果中以'LISTEN'后跟0、 1或多个空白字符结尾的行
[root@wzc date]# netstat -tan |grep "\bLISTEN[[:space:]]*$"
tcp        0      0 192.168.122.1:53        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     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN
12、添加用户bash、 testbash、 basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行
[root@wzc date]# useradd bash
[root@wzc date]# useradd testbash
[root@wzc date]# useradd basher
[root@wzc date]# useradd nologin -s /sbin/nologin 
[root@wzc date]#  getent passwd |grep "^\(\b.*\b\).*\<\1\>$"
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:1017:1026::/home/bash:/bin/bash
nologin:x:1020:1029::/home/nologin:/sbin/nologin
13、显示当前系统root、 mage或wang用户的UID和默认shell
[root@wzc date]# getent passwd|grep -e "^root\b" -e "^mage\b" -e "^wang\b"|cut -d: -f3,7
0:/bin/bash
1009:/bin/bash
1010:/bin/bash
14、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
[root@wzc date]# grep -E "^([[:alpha:]_])+\(\).*" /etc/rc.d/init.d/functions 
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
15、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@wzc date]# echo /etc/rc.d/init.d/functions |egrep -o "[^/]+$"
functions
16、使用egrep取出上面路径的目录名
[root@wzc date]# echo "/etc/rc.d/init.d/functions" | egrep -o "^.*/"
/etc/rc.d/init.d/
17、统计以root身份登录的每个远程主机IP地址的登录次数
[root@wzc date]# last |grep root|tr -s " "|cut -d" " -f3|grep -v ":"|uniq -c|tail -n1
     23 10.1.250.38
18、利用扩展正则表达式分别表示0-9、 10-99、 100-199、200-249、 250-255
[root@localhost ~]# 0-9:[0-9]; 10-99:[1-9][0-9]; 100-199:[1-9][0-9]{2}; 200-249:2[0-4][0-9]; 250-255:25[0-5]
19、用正则表达式表示手机号(11 13 17 15  18 )
[root@wzc date]# cat xiaomi 
11568973427
13546789524
15789342569
17789828269
18217968392
[root@wzc date]# egrep -o "1[13578][0-9]{9}" xiaomi
11568973427
13546789524
15789342569
17789828269
18217968392
22、使用cut把本机ip地址提取出来
  centos7版本
[root@wzc date]# ifconfig |head -n2 |tail -n1|cut -d. -f1-4|cut -dt -f2|cut -dn -f1
 10.1.253.22
  centos6版本
[root@localhost ~]# ifconfig |head -n 2|tail -n 1|cut -d: -f2|cut -dB -f1
10.1.253.24

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

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

相关推荐

  • FHS文件系统以及各目录功能

    FHS:Filesystem Hierarchy Standard(文件系统目录标准)的缩写,多数Linux版本采用这种文件组织形式,类似于Windows操作系统中c盘的文件目录,FHS采用树形结构组织文件。FHS定义了系统中每个区域的用途、所需要的最小构成的文件和目录,同时还给出了例外处理与矛盾处理,规范在根目录(/)下面各个主要目录应该放什么样的文件。 …

    Linux干货 2016-10-17
  • MySQL复制: Galera

    MySQL复制: Galera mysql 主从复制 前言 Galera Replication简介 MariaDB-Galera-Server 环境部署 配置步骤 总结 前言 之前介绍了MySQL复制的各种解决方案, 但是我个人还是感觉Galera最好用也最实用, 什么是Galera, 它强大在哪里, 这篇文章就带你认识这个强大的工具 Galer…

    Linux干货 2016-04-28
  • IO,用户与组管理,文件,目录权限管理

           文件统配匹配模式:元字符文件名通配符*匹配任意长度的任意字符[root@localhost ~]# ls /root/D*/root/Desktop  /root/Documents  /root/Downloads ?匹配单个任意字符[root@localhost ~]# …

    Linux干货 2016-08-05
  • 集中练习7-bash脚本

    集中练习7-bash脚本

    Linux干货 2017-12-05
  • yum详解及源码包的编译安装

    yum的功能:     在我们使用rpm包安装程序时,各个程序包之间可能存在非常严重的依赖关系,这就导致我们在安装一个包的同时还需要安装其他存在依赖关系的包,因此我们可以使用rpm包的前端管理工具yum,可通过yum所支持的文件共享机制来解决各个rpm包之间的依赖关系。 yum仓库的配置文件:   …

    Linux干货 2016-08-25
  • Shell脚本编程基础之一

      Linux为高效管理系统,提供了CLI命令行接口,供用户在命令提示符下输入命令,它有很多bash shell基础特,根据这些特性能够很灵活的使用命令,也支持使同时输入多个命令执行,命令之间用冒号分隔;但是要完成复杂多次重复性的操作,非常不便,系统管理工作变得很是繁忙,很难轻松完成工作任务;不过Linux的shell支持脚本编程,通过编写纯文本格…

    Linux干货 2016-08-15