文本处理练习题

文本处理练习:

 

1.找出本机ip地址

  [root@localhost ~]# ifconfig |head -2 |tail -1 |tr -s ' ' ':' |cut -d: -f3

  10.1.252.221

 

2.查看本机分区最大的利用率

  [root@localhost ~]# df |tr '%' ' ' |tr -s ' ' ':' |cut -d: -f5 |tr '[:alpha:]' ' ' |sort |tail -1

  29

 

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

  [root@localhost ~]# getent passwd |cut -d: -f1,3,7 |sort -t: -k2 -n |tail -1

  nfsnobody:65534:/sbin/nologin

 

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

  [root@localhost ~]# stat /tmp |head -4 |tail -1 |tr -s ' ' ':' |cut -d: -f2 |tr -cd '[:digit:]'

  1777

 

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

  [root@localhost ~]# netstat -nt |grep 'tcp' |tr -s ' ' ':' |cut -d: -f4 |uniq -c

      5 10.1.252.221

 

 

 

正则表达式练习:

 

1.显示/proc/meminfo文件中以大小写s开头的行

 

[root@localhost home]# grep -i '^s' /proc/meminfo

SwapCached:            0 kB

SwapTotal:       2098172 kB

SwapFree:        2098172 kB

Shmem:              6992 kB

Slab:              70944 kB

SReclaimable:      41568 kB

SUnreclaim:        29376 kB

 

[root@localhost home]# grep '^[Ss]' /proc/meminfo

SwapCached:            0 kB

SwapTotal:       2098172 kB

SwapFree:        2098172 kB

Shmem:              6992 kB

Slab:              70944 kB

SReclaimable:      41568 kB

SUnreclaim:        29376 kB

 

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

 

[root@localhost ~]# 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

……

 

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

 

[root@localhost ~]# grep '^rpc\>' /etc/passwd |cut -d: -f7

/sbin/nologin

 

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

 

[root@localhost ~]# grep -w '[[: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

nobody:x:99:99:Nobody:/:/sbin/nologin

……

 

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

 

[root@localhost ~]# grep '^[[:space:]]\+.*[^[: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=""

 

 

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

 

[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    

tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN

 

 

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

 

[root@localhost ~]# getent passwd |grep '^\<\(.*\)\>.*\b\1\b$'

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 magewang用户的UID和默认shell

 

[root@localhost ~]# getent passwd |grep -E '^\<(root|mage|wang)\>' |cut -d: -f3,7

0:/bin/bash

1005:/bin/bash

1006:/bin/bash

 

2、找出/etc/rc.d/init.d/functions文件中行首为某单词(

括下划线)后面跟一个小括号的行

 

[root@localhost ~]# grep -E '^(_|[[:alpha:]])+\(' /etc/init.d/functions

checkpid() {

__pids_var_run() {

__pids_pidof() {

daemon() {

killproc() {

pidfileofproc() {

pidofproc() {

status() {

 

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

 

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

functions

 

 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 -o '^root\>.*([[:digit:]]\.)[[:digit:]]+' |tr -s ' ' |cut -d" " -f1,3 |sort |uniq -c

     15 root 10.1.250.107

      1 root 192.168.1.104

 

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

 

[0-9]

[1-9][0-9]

1[0-9]{2}

2[0-4][4-9]

25[0-5]

 

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

 

[root@localhost ~]# ifconfig |egrep -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.252.221

255.255.0.0

10.1.255.255

127.0.0.1

255.0.0.0

 

 

 

 

 

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

(0)
JaggerJagger
上一篇 2016-08-08 16:15
下一篇 2016-08-08 16:16

相关推荐

  • N-22-南京-修 第二周作业

    linux文件管理命令有:cp,mv,rm cp命令:用于复制件或目录文 [root@localhost etc]# cp -i /etc/passwd /tmp/123 [root@localhost etc]# cd /tmp [root@localhost tmp…

    Linux干货 2016-08-22
  • Linux的命令使用格式

    ◆Linux命令格式:command [options]  [arguments]command:命令options:  –单词   或   -单字如: ls –allequ      ls -als -a -b -cequ&…

    Linux干货 2016-10-31
  • vsftpd基于mysql进行虚拟用户管理

    概述:     FTP是我们日常工作中经常用到的一个服务,但是FTP对用户的管理却比较薄弱,默认状态下,FTP利用pam机制进行账号管理,默认情况下使用的是系统账号,如何提升FTP对用户管理的有效性,针对不同的用户设定不同的上传权限,这就要基于虚拟账号来管理了,本篇就介绍下在vsftpd利用pam机制,结合mysql实…

    Linux干货 2016-10-18
  • 网络管理(三)之路由设置、网络组

    网络管理(三)之路由设置、网络组 一、网卡名称: 1、网络接口的识别命名udev配置文件:#vim /etc/udev/rules.d/70-persistent-net.rules 2、网卡查看:#dmesg | grep -i eth 或者#ethtool -i eth0            …

    Linux干货 2016-09-08
  • vsftpd+pam+MySQL—->实现虚拟用户认证

    一、安装所需要程序 1、安装vsftpd yum -y install vsftpd 2、安装MySQL yum -y install  mysql-server mysql-devel pam_mysql 二、创建虚拟用户账号 1.准备数据库及相关表 首先请确保mys…

    Linux干货 2016-09-19
  • Linux基础知识(六)-vim编辑器,crontab计划任务,bash脚本循环

    1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp [root@localhost ~]# vim&nbs…

    Linux干货 2016-10-31