文本处理课后小练习

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)
上一篇 2016-08-08 16:05
下一篇 2016-08-08 16:05

相关推荐

  • ArchLinux折腾记

    刚接触linux的时候, 听到前辈说到这样一个系统,  说是linux 技能纯熟才能驾驭的了ArchLinux , 最近正好有个半天闲, 于是抽空,  佐以官方文档,  来认认真真的折腾一下ArchLinux Contents ArchLinux简介  准备工作  分区设置  基本功能安装 &nbs…

    Linux干货 2016-09-29
  • Linux基础学习总结(一)

    Linux 安装运行步骤: 进入\\172.16.0.1\python10          用户名:python10          密码:python10magedu 进入目录   ftp://172.16.0.1/pub/ISOs/ 下载linux系统镜像文件           CentOS-6.9-x86_64-bin-DVD1.iso     …

    Linux干货 2018-03-16
  • 马哥教育21期网络班—第11周课程+练习—-成长进行时–不退步–下

    3、描述DNS查询过程以及DNS服务器类别。 DNS查询过程主机—>查找互联网上的主机 www.magedu.com—>联系到自己指向的DNS服务器—>DNS服务器—>找根 .—>根返回给DNS服务器—>DNS找.com—>com返回给DNS—->DNS找ma…

    Linux干货 2016-09-19
  • 编译内核——制作最适合自己系统的内核

    编译内核: ·程序包的编译安装:         ./configure ,make ,make install ·前提:开发环境(开发工具、开发库),头文件:/usr/include ·开源:源代码–>可执行格式    &nbsp…

    Linux干货 2016-09-11
  • Gay Video Chat Sites – Personals

    Bromodates Men Seeking Men eHarmony.com Adam4Adam.com FabGuys.com Grindr Taimi GayCupid.com Scruff Hornet Gaydar Bromodates – Popular gay hookup app internationally You will …

    Linux干货 2023-02-21
  • 一起学DNS系列(十五)DNS查询工具之NSLOOKUP的使用

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeffyyko.blog.51cto.com/28563/259092    上一节里我们讨论了有关DIG工具的用法,本节将对windows下nslookup工具的一些主要命令进行描述。  …

    2015-03-17