文本处理工具作业

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

root@cenots6.8  ~ #  ifconfig | tr -cs '[0-9]\.' '\n' |sort -u -t'.' -k3n

127.0.0.1
192.168.1.103
192.168.1.255
255.255.255.0

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

root@cenots6.8  ~ # df | tr -s ' '|cut -d' ' -f5|sort -n|tail -1|tr -d %
19

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

root@cenots6.8  ~ # cat /etc/passwd|cut -d: -f1,3,7|sort -n -t: -k2 |tail -1
nfsnobody:65534:/sbin/nologin

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

root@cenots6.8  ~ # stat /tmp|head -4|tail -1 | tr -cs [0-9] '\n'|head -2|tail -1
1777

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

root@cenots6.8  ~ # netstat -nt|tail -3|tr -s " "|cut -d" " -f5|cut -d: -f1|sort -n|uniq -c
      1 111.108.54.43
      2 192.168.1.102

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

root@cenots6.8  ~ # grep '^[sS]' /proc/meminfo;grep -i '^s' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              3300 kB
Slab:             112308 kB
SReclaimable:      42212 kB
SUnreclaim:        70096 kB
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              3300 kB
Slab:             112308 kB
SReclaimable:      42212 kB
SUnreclaim:        70096 kB

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

root@cenots6.8  ~ # 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

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

root@cenots6.8  ~ # grep 'rpc\>' /etc/passwd |cut -d: -f7
/sbin/nologin

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

root@cenots6.8  ~ #  grep -o  "\<[0-9]\{2,3\}\>" /etc/passwd

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

root@cenots6.8  ~ # grep "^[[:space:]]\+[^[:space:]]\+" /etc/grub.conf
    root (hd0,0)
    kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=8ecfb3ed-37d8-43cd-a1ec-8a4be6fa5973 nomodeset rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-642.el6.x86_64.img

6、找出"netstat -tan"命令的结果中以'LISTEN'后跟0、 1或多个空白字符结尾的行

[root@localhost ~]# netstat -tan | grep "LISTEN[[: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

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

[root@localhost ~]# grep "^\(\<[[:alnum:]]\+\)\>.*\1$" /etc/passwd
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:3011:3016::/home/bash:/bin/bash
nologin:x:3014:3019::/home/nologin:/sbin/nologin

1、显示当前系统root、 mage或wang用户的UID和默认shell

root@cenots6.8  ~ # egrep "^(root|mage|wang)\>" /etc/passwd |cut -d: -f3,7
0:/bin/bash
503:/bin/bash
504:/bin/bash

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

root@cenots6.8  ~ # egrep "^[[:alpha:]]+\(\)|^\_+\(\)" /etc/rc.d/init.d/functions
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
confirm() {

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

root@cenots6.8  ~ # echo "/etc/rc.d/init.d/functions" | egrep -o  "\<[[:alpha:]]+$"
functions

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

root@cenots6.8  ~ # echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/"
/etc/rc.d/init.d/

5、统计以root身份登录的每个远程主机IP地址的登录次数

root@cenots6.8  ~ #  last | egrep -o "^root\>.*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"|tr -s " "|cut -d" " -f3|sort|uniq -c
      4 10.1.250.89
      3 192.168.1.102
      5 192.168.1.103
      1 192.168.1.104

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

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]

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

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])'
192.168.1.103
192.168.1.255
255.255.255.0
127.0.0.1
255.0.0.0

1、取本机ip地址

root@cenots6.8  ~ # ifconfig|egrep -o  "inet addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" |cut -d: -f2|head -1
192.168.1.103

2、取各分区利用率的数值

root@cenots6.8  ~ # df | tr -s " "|cut -d" " -f5
Use%
4%
1%
19%

3、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

cat /etc/init.d/functions | tr -cs "[:alpha:]" "\n"|sort|uniq -c|sort -n

4、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/"  取目录名

root@cenots6.8  ~ # echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/"
/etc/rc.d/init.d/

5、正则表达式表示身份证号

root@cenots6.8  /testdir # egrep -io "[0-9]{17,18}x?" card
421081199402080612
350722198609015732
440202198004250317
420321198709043810

6、正则表达式表示手机号

root@cenots6.8  /testdir # egrep -o "1[34578][0-9]{9}" iphone
13901212122
13718063333
13611111919
13021218888
13051688888
13552709999

7、正则表达式表示邮箱

root@cenots6.8  /testdir # egrep "^[^@]+@.*(com|cn)$" mail 
530180782@qq.com
243678025@qq.com
398018489@qq.com
595064131@qq.com
362483245@qq.com

8、正则表达式表示QQ号

root@cenots6.8  /testdir # egrep "[1-9][0-9]{4,10}" mail -o

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

(0)
NarutoNaruto
上一篇 2016-08-07 14:17
下一篇 2016-08-07 22:05

相关推荐

  • Linux基础操作-week5

    1、显示当前系统上root、fedora或user1用户的默认shell; 方式一: # whoami root # echo $SHELL /bin/bash 方式二: #grep user1 /etc/passwd|awk -F “:” ‘{print $7}’ /bin/bash 2、找出/etc/rc.…

    Linux干货 2016-11-27
  • Linux基础知识之cp mv rm

    该博文以CentOS6.8_x86_64系统为基础,Xshell 5远程连接CentOS系统,以root身份登录系统和sjsir普通用户身份登录系统。 为什么要学习cp、mv、rm命令?     cp、mv、rm命令为Linux系统使用最长使用的三个命令之一,复制、移动和删除是我们平时处理一些文件必须要学会的命令,应该用于熟练掌握和使用的…

    Linux干货 2016-07-29
  • mysql并发控制

    并发控制: 锁:Lock 1、锁类型 : 读锁:共享锁,可被多个读操作共享; 写锁:排它锁,独占锁; 2、锁粒度: 表锁:在表级别施加锁,并发性较低; 行锁:在行级另施加锁,并发性较高; 3、锁策略:在锁粒度及数据安全性之间寻求一种平衡机制; 存储引擎:级别以及何时施加或释放锁由存储引擎自行决定; MySQL Server:表级别,可自行决定,也允许显式请求…

    2016-11-18
  • ansible学习笔记

    简介:  在日常服务器维护中,从系统安装到程序部署再到发布应用,在大规模的生产环境中,如果需要手动的每台服务器进行安装配置将会给运维人员带来许多繁琐而又重复的工作。这就促使了在每个运维层次中出现了不同的自动化运维工具。 常见的自动化运维工具分类有以下几类:  系统安装运维工具(OS Provisioning):    …

    Linux干货 2015-08-17
  • 如何解决生产机上php代码连接mysql报错的故障

    大家好: 今天分享一则当生产机上的网站php代码不能连接Mysql服务器时怎么办? 当LNMP的网站建立好后,我们需要测试网站中的php代码。但发现如下报错怎么办? 解决方法如下:  mysql> show databases; +——————–+ | Dat…

    Linux干货 2016-12-18
  • Linux进程及作业管理

    process 运行中的程序的一个副本,是被载入内存的一个指令集合 进程ID(Process ID,PID)号码被用来标记各个进程 UID、GID、和SELinux语境决定对文件系统的存取和访问权限, 通常从执行进程的用户来继承 存在生命周期 task struct:Linux内核存储进程信息的数据结构格式 task list:…

    Linux干货 2016-09-11