作業–文本處理工具

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.www58058.com/31059

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

相關推薦

  • Linux下常用安全策略設置的六個方法

    安全第一”對于linux管理界乃至計算機也都是一個首要考慮的問題。加密的安全性依賴于密碼本身而非算法!而且,此處說到的安全是指數據的完整性,由此,數據的認證安全和完整性高于數據的私密安全,也就是說數據發送者的不確定性以及數據的完整性得不到保證的話,數據的私密性當無從談起! 1. 禁止系統響應任何從外部/內部來的ping請求攻擊者一般首先通過ping命令檢測此…

    Linux干貨 2017-07-31
  • 文件查找命令之find

    文件查找命令之find   特點:實時查找,精確查找,由于find是全磁盤文件查找所有查找速度要比locate略慢一些。   find查找功能強大,下面主要介紹find查找條件的一個重要特性–德·摩根定律     德·摩根定律      非(A &&B)=(非A…

    Linux干貨 2016-08-16
  • N26 第二周

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。  [1]mkdir : make directories            mkdir [OPTION]… DIRECTORY… &n…

    Linux干貨 2017-02-05
  • Bonding多塊網卡綁定同一IP地址

    Bonding多塊網卡綁定同一IP地址 就是將多塊網卡綁定同一IP地址對外提供服務,可以實現高可用或者負載均衡。當然,直接給兩塊網卡設置同一IP地址是不可能的。通過bonding,虛擬一塊網卡對外提供連接,物理網卡的被修改為相同的MAC地址。 一共有七種模式這里提供三種常用的:   一、Mode 0 (balance-rr)  輪轉( Round-robin…

    Linux干貨 2016-09-07
  • 【N25第四周作業】grep

    文本處理工具: Linux上文本處理三劍客: grep, egrep, fgrep:文本過濾工具(模式:pattern)工具; grep:基本正則表達式,-E,-F egrep:擴展正則表達式, -G,-F grep:不支持正則表達式, sed:stream editor, 流編輯器;文本編輯工具; awk:…

    Linux干貨 2016-12-18
  • linux上的文件查找工具:locate和find

    一、非實時超找工具 locate     1、locate是查詢系統上預建的文件索引數據庫/var/lib/mlocate/mlocate.db文件索引實在系統比較空閑的時候自動進行的,是系統的周期性任務,因為是周期性任務,所以有些短期的數據無法更新到數據庫,如果需要馬上應用,乣管理員手動更新數據庫,用命令updatedb來執行。同時索引…

    Linux干貨 2016-08-16
欧美性久久久久