文本处理课后小练习

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

相关推荐

  • 编译安装——吐血经验,内附脚本

    程序包编译安装: 源码包:name-VERSION-release.src.rpm         rpm由源码包安装后,使用rpmbuild命令制作成二进制格式的rpm包,而后再安装        &nbsp…

    Linux干货 2016-08-24
  • 第十周作业

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) 2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;   (1) 为硬盘新建两个主分区;并为其安装grub; #创建两个分区,/dev/sdb1为500M,/dev/sdb2为5G [root@centos6 mnt]# fdisk -l /dev/sdb…

    2017-03-12
  • web服务介绍二)

    apache指令说明:http://httpd.apache.org/docs/2.2/mod/directives.html Listen 172.18.100.67:8081  监听某个指定地址和端口启动前检查一下语法:    httpd -t 如果要监听多个端口,要多行写    Listen 80&nb…

    Linux干货 2017-04-20
  • linux目录基础、简单的命令以及运用linux帮助

    计算机组成及其功能     计算机其实是:接收用户输入指令与数据,经过中央处理器的数据与逻辑单元运算处理后,以产生或存储成有用的信息。所以计算机组成可以分为输入单元、输出单元、CPU(控制单元、算术逻辑单元)、内存     输入单元:包括键盘、鼠标、扫描仪、手写板、触摸屏等;实现…

    Linux干货 2016-09-17
  • 8月9日sed(更新版)

    sed命令及vim基础使用命令 处理文本的工具sed stream editor,行编辑器    sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有…

    Linux干货 2016-08-15
  • grep命令

    ]# chmod -R 700 /home/tuser1 ]# ll /home/tuser1/ total 0 drwx——. 2 root root 72 Nov 20 17:33 skel 新增用户与组: 显示/proc/meminfo: 显示shell 非 /sbin/nologin 的用户 找出/etc/passwd文件中一…

    Linux干货 2016-11-20