文本处理工具作业

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)
上一篇 2016-08-07 14:17
下一篇 2016-08-07 22:05

相关推荐

  • 运行级别

    运行级别(Runlevel)指的是Unix或者Linux等类Unix操作系统下不同的运行模式。运行级别通常分为7等,分别是从0到6,但如果必要的话也可以更多。 例如在大多数Linux操作系统下一共有如下7个典型的运行级别: 0 停机,关机 1 单用户,无网络连接,不运行守护进程,不允许非超级用户登录 2 多用户,无网络连接,不运行守护进程 3 多用户,正常启…

    Linux干货 2017-07-10
  • N25-第五周作业

    第五周 1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@zf ~]# grep -E "^[[:space:]]+[[:alnum:]]+" /boot/grub/grub.conf     &n…

    Linux干货 2016-12-26
  • puppet学习笔记

      一、Puppet基础原理: Puppet是一款使用GPLV2X协议授权的开源管理配置工具,用ruby语言开发,既可以通过客户端—服务器的方式运行,也可以独立运行。puppet可以为系统管理员提供方便,快捷的系统自动化管理。   二、puppet工作流程 1. 客户端 puppet-client 向 puppet-maste…

    Linux干货 2015-11-04
  • Bonding多块网卡绑定同一IP地址

    Bonding多块网卡绑定同一IP地址 就是将多块网卡绑定同一IP地址对外提供服务,可以实现高可用或者负载均衡。当然,直接给两块网卡设置同一IP地址是不可能的。通过bonding,虚拟一块网卡对外提供连接,物理网卡的被修改为相同的MAC地址。 一共有七种模式这里提供三种常用的:   一、Mode 0 (balance-rr)  轮转( Round-robin…

    Linux干货 2016-09-07
  • tomcat

    编程语言: 硬件级:微码编程,汇编语言 系统级:C,C++,… 应用级:Java, PHP, Python, Ruby, Perl, C#, … 程序:指令+数据 过程式编程:以指令为中心,数据服务于代码; 对象式编程:以数据为中心,指令服务于数据; java Java代码的运行:*.java(source code) –…

    Linux干货 2017-07-03