文本處理工具(練習+作業)

文本處理工具(cut,sort,uniq)練習

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

[root@localhost ~]# ifconfig | tr -cs '[:digit:].' '\n'| sort -t. -k3 |tail -5

1.png

2、查出分區空間使用率的最大百分比值

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

2.png

3、查出用戶UID最大值的用戶名、UID及shell類型

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

3.png

4、查出/tmp的權限,以數字方式顯示

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

4.png

5、統計當前連接本機的每個遠程主機IP的連接數,并按從大到小排序

[root@localhost ~]# netstat -nt |tr -s ' ' ':' |cut -d: -f6 | tr -d '[:alpha:]' | sort -r | uniq -c | sort -r

5.png


grep正則表達式練習

1、顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式)

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

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

G1.png

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

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

G2.png

3、顯示用戶rpc默認的shell程序

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

G3.png

4、找出/etc/passwd中的兩位或三位數

[root@localhost ~]# grep "\<[0-9]\{2,3\}" /etc/passwd

G4.png

5、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行

[root@localhost ~]# grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg

G5.png

6、找出"netstat -tan"命令的結果中以'LISTEN'后跟任意多個空白字符結尾的行

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

G6.png

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

[root@localhost ~]# grep "^\([^:]\+\>\).*/\1$" /etc/passwd

G7.png

egrep擴張正則表達式


1、顯示三個用戶root、mage、wang的UID和默認shell

[root@localhost ~]# egrep "^(root|mage|wang)\>" /etc/passwd | cut -d -f1,3,7

EG1.png

2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行

[root@localhost ~]# egrep "[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions

EG2.png

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

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

EG3.png

4、使用egrep取出上面路徑的目錄名

[root@centos7 ~]# echo "/etc/rc.d/init.d/functions/" | egrep -o "/.*[^/]" | egrep -o "/.*/"

EG4.png

5、統計以root身份登錄的每個遠程主機IP地址的登錄次數

[root@centos7 ~]# last |grep "^root\>"| tr -s " " | cut -d" " -f3 | grep "^[[:digit:]]" | sort -t. -k4 -n | uniq -c

1470460117810650.png

6、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255

0-9  :   [0-9]

10-99 :   [1-9][0-9]

100-199 :   1[0-9][0-9]

200-249 :   2[0-4][0-9]

250-255 :   25[0-5]

7、顯示ifconfig命令結果中所有IPv4地址

[root@centos7 ~]# ifconfig | egrep -o "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"

1470460158447113.png

課后作業

1、取本機ip地址

[root@localhost ~]# ifconfig | egrep -o "\<inet[[:space:]]*(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>" | cut -d' ' -f2

Z1.png

2、取各分區利用率的數值

[root@localhost ~]# df | grep "^/dev/sd" | tr -s ' ' ':'| cut -d: -f1,5 | tr -d '%'

Z2.png

3、統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示

[root@localhost ~]# cat /etc/init.d/functions | tr -cs '[:alpha:]' "\n" | sort |uniq -c|  sort -n -r

Z3.png

4、正則表達式表示身份證號

[1-9][0-9]{5}:前6位數字

(19[0-9][0-9]|200[0-9]|201[0-6]):4位年份

(0[0-9]|1[0-2]):2位月份

([0-2][0-9]|3[0-1]):2位日期

[0-9]{3}([0-9]|X):后四位隨機數(包含出現的X情況)

[root@localhost ~]# echo "ID CARD :41071119940402301X" | egrep -o "\<[1-9][0-9]{5}(19[0-9][0-9]|200[0-9]|201[0-6])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])[0-9]{3}([0-9]|X)\>"

Z4.png

5、正則表達式表示手機號

[root@localhost ~]# echo "Phone number : 18888888888" | egrep -o "\<1(3|5|7|8)[0-9]{9}\>"

Z5.png

6、正則表達式表示郵箱

郵箱格式:用戶名@服務器域名

[root@localhost ~]# grep -o "\<[[:alnum:]]\+@[[:alnum:]]\+\.com" mail

Z6.png

Z6.1.png

7、正則表達式表示QQ號

QQ號5-10位

[1-9]:第一個數字不能為0

[0-9]{4,9}:后面跟4-9位隨機數

egrep -o "\<[1-9][0-9]{4,9}\>"

Z7.png

原創文章,作者:Groot,如若轉載,請注明出處:http://www.www58058.com/30027

(0)
GrootGroot
上一篇 2016-08-07
下一篇 2016-08-07

相關推薦

  • Linux 文 本 處 理 工 具

    Linux 文 本 處 理 工 具 一.學習大綱: ◎各種文本工具來查看、分析、統計文本文件 文件內容查看工具:cat, tac,rev,more,less 文件截取:head和tail 按列抽?。篶ut,paste 分析文本的工具:wc , sort , uniq,diff和patch 命令使用練習題 ◎文本過濾與處理工具: grep與正則表達式…

    Linux干貨 2016-08-05
  • OpenStack Glance安裝配置過程記錄

    Glance是作為OpenStack的虛擬機的Image(鏡像)服務, 它提供了一系列的REST API, 用來管理、查詢虛擬機的鏡像, 它支持多種后端存儲介質, 例如用本地文件系統作為介質、Swift(OpenStack Object Storage)作為存儲介質或者S3兼容的API作為存儲介質。 Glance作為OpenStack的一個核心的系統, 被設…

    2017-09-14
  • 淺談Linux終端類型

    Linux終端類型 作者:任飛鵬            日期:2016-10-13 終端是什么: 終端(Terminal)也稱終端設備,是計算機網絡中處于網絡最外圍的設備,主要用于用戶信息的輸入以及處理結果的輸出等。 早期計算機系統中,由于計算機主機…

    Linux干貨 2016-10-19
  • 第四周

    查看鏈接:http://note.youdao.com/noteshare?id=317ec635a5f28f2681421fd7c9a60f27

    Linux干貨 2016-09-19
  • 看了還想看—普通權限及umask

        權限在操作系統是尤為重要的,無論是windows和linux中,都少不了權限這么一說,權限的大小決定了你能操作些什么,在linux中,權限對目錄和文件的意義是不同的,并且還有特殊權限。今天,我們就來說說一說權限在linux中的重要性以及權限操作命令。     權限:權限決…

    Linux干貨 2016-08-03
  • Linux文本處理三劍客之grep

    一、grep命令 grep(global search regular expression(RE) and print out the line,全面搜索正則表達式并把行打印出來 作用:文本搜索工具,根據用戶指定的“模式”對目標文本逐行進行匹配檢查;打印匹配到的行。 模式:由正則表達式字符及文本字符所編寫的過濾條件 二、grep命令格式 grep [OPT…

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