作业–文本处理工具

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

[root@liang ~]# ifconfig        #centos6下
eth0      Link encap:Ethernet  HWaddr 00:0C:29:BA:F9:36  
          inet addr:192.168.99.99  Bcast:192.168.99.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:feba:f936/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3670 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2963 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:341489 (333.4 KiB)  TX bytes:354417 (346.1 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:600 (600.0 b)  TX bytes:600 (600.0 b)
          
[root@fengl ~]# ifconfig        #centos7下
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.99.100  netmask 255.255.255.0  broadcast 192.168.99.255
        inet6 fe80::20c:29ff:fe01:b00c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:01:b0:0c  txqueuelen 1000  (Ethernet)
        RX packets 2804  bytes 242061 (236.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1855  bytes 201673 (196.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 852  bytes 68852 (67.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 852  bytes 68852 (67.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
方法一:
[root@liang ~]# ifconfig | grep Mask | tr -s ' '|cut -d' ' -f3|cut -d':' -f2    #centos6下
[root@fengl ~]# ifconfig | grep netmask | tr -s ' '|cut -d' ' -f3      #centos7下
方法二(通用):
[root@fengl ~]# 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])'

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

[root@liang ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       41153856 2146548  36910156   6% /
tmpfs             953140       0    953140   0% /dev/shm
/dev/sda1        1998672   40260   1853556   3% /boot
[root@liang ~]# df | tr -s ' '|cut -d ' ' -f 5|tr -d %|sort -n|tail -1

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

[root@fengl ~]# getent passwd | sort -t : -k 3 -n | cut -d : -f 1,3,7|tail -1

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

[root@fengl ~]# stat /tmp
  File: ‘/tmp’
  Size: 4096          Blocks: 8          IO Block: 4096   directory
Device: 802h/2050d    Inode: 786433      Links: 7
Access: (1777/drwxrwxrwt)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:tmp_t:s0
Access: 2016-07-11 19:00:12.017347722 +0800
Modify: 2016-08-08 18:58:01.445117085 +0800
Change: 2016-08-08 18:58:01.445117085 +0800
 Birth: -
[root@fengl ~]# stat /tmp | head -4|tail -1|cut -d '(' -f2|cut -d '/' -f1

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

[root@fengl ~]# netstat -nt|grep tcp|tr -s ' '|cut -d ' ' -f 5|cut -d : -f 1|uniq -c|sort -nr

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

[root@fengl ~]# cat /proc/meminfo |grep -i ^s     #方法1   
[root@fengl ~]# cat /proc/meminfo |grep -E '^s|^S'    #方法2
[root@fengl ~]# cat /proc/meminfo |grep '^[sS]'    #方法3

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

[root@fengl ~]# cat /etc/passwd | grep -v '/bin/bash$'

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

[root@fengl ~]# cat /etc/passwd | grep '^rpc\>'|cut -d : -f 7

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

[root@fengl ~]# grep -E '\<[1-9][0-9]\>|\<[1-9][0-9]{2}\>' /etc/passwd

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

[root@fengl ~]# grep '^[[:space:]]\+[^[:space:]]' /etc/grub2.cfg 
[root@fengl ~]# grep -E '^[[:space:]]+[^[:space:]]' /etc/grub2.cfg

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

[root@fengl ~]# netstat -tan | grep 'LISTEN[[:space:]]*$'

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

[root@fengl home]# cat /etc/passwd |grep '^\<\(.*\)\>.*\b\1\b$'

13、显示三个用户root、mage、wang的UID和默认shell

[root@fengl home]# grep -E '^(root|mage|wang)' /etc/passwd | cut -d: -f3,7

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

[root@fengl home]# grep '^\<[[:alpha:]_]\+\>()' /etc/rc.d/init.d/functions

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

[root@fengl home]# echo "/etc/rc.d/init.d/functions" | grep -E -o '[^/]+/?$'

16、使用egrep取出/etc/rc.d/init.d/functions的目录名

[root@fengl home]# echo "/etc/rc.d/init.d/functions" | grep -E -o '^/.*/'

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

[root@fengl ~]# last | grep -E -o "^root\>.*((([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]))" | tr -s ' '|cut -d ' ' -f3|uniq -c

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

[root@liang6 /]# cat /etc/init.d/functions | tr -cs '[:alpha:]' '\n'|sort|uniq -c|sort -t' ' -k1 -nr

19、从文件haoma里正则表达式表示身份证号

[root@liang6 /]# grep -E '\b[1-9][0-9]{5}(18|19|20)[0-9]{2}(0[1-9]|1(0-2))(0[1-9]|[12][0-9]|3[0-1])[0-9]{3}[0-9X]\b' haoma

20、从文件haoma里使用正则表达式表示手机号

[root@liang6 /]# grep -E -o "(\+86)?1[38][0-9]{9}|14[57][0-9]{8}|15[0-35-9][0-9]{8}|17[0678][0-9]{8}" haoma

21、从文件haoma里正则表达式表示邮箱

[root@liang6 /]# grep -E '\b[[:alnum:]][[:alnum:]\_-]*@[[:alnum:]][[:alnum:]\.\_-]*\b' haoma

22、从文件haoma里正则表达式表示QQ号

[root@liang6 /]# grep -E -o '\b[1-9][0-9]{4,11}\b' haoma

原创文章,作者:苦涩咖啡,如若转载,请注明出处:http://www.178linux.com/31059

(0)
苦涩咖啡苦涩咖啡
上一篇 2016-08-10
下一篇 2016-08-10

相关推荐

  • linux获取”命令帮助“

    一、相关术语:         外部命令:一个可执行程序,位于文件系统某目录下;是系统为了完善各种功能而加入的程序。         内部命令:系统shell程序的部分程序。内部命令执行速度非常快,其…

    Linux干货 2016-06-09
  • 压缩和解压缩

    今天我们所讲一些压缩以及解压缩的内容,可以方便我们去传输一些数据较大的文件,以及可以降低我们的内存使用率, 节省空间。我们常用的工具对单个文件进行压缩有4种,对于多个文件进行打包压缩有2种。 1、compress(单个文件压缩)     压缩后的文件后缀名都是.Z结尾     compress fi…

    2017-08-12
  • 源码包编译安装

    程序包编译安装:      Application-VERSION-release.src.rpm–> 安装后,使用rpmbuild命令制作成二进制格式的rpm包,而后再安装      源代码 –> 预处理 –> 编译 &#8…

    Linux干货 2016-08-23
  • 【知识点】NO.1—-关于删除文件后未及时释放磁盘空间问题

    删除了某些占用大存储的文件,发现占用的磁盘空间并没有的到释放。怎么解决呢?
    在工作中经常遇到服务器某个存放日志或者某些数据的磁盘(或者目录),因为检查不及时磁盘存储满了的状况。释放空间真的只是删除文件这么简单吗?nonono…

    2017-11-18
  • N24 W3 博客作业

    第三周 "   1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# who | cut –d’ ‘ –f1 | uniq 2、取出最后登录到当前系统的用户的相关信息。 [root@localhost ~]# id `who | tail -n 1 | cut …

    Linux干货 2016-11-14
  • Linux 的shell脚本编程

    shell脚本编程 程序:指令+数据 程序编辑风格:             过程式:以指令为中心,数据服务于指令             对象式:以数据为中心,指令服务于数据 shell程序:提供了编程能力,解释执行 计算机:运行二进制指…

    Linux干货 2016-08-15