文本處理工具-習題

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

[root@centos7 ~]# ifconfig |head -2 |tail-1 |cut -dn -f2 |cut -d" " -f2

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

[root@centos7 ~]# df |cut -c44-46 |sort -n|tail -2 |head -1

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

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

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

[root@centos7 ~]# stat /tmp |head -4 |tail-1 |cut -d/ -f1 |cut -d\( -f2

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

# netstat -tn |cut -d: -f2|tr -s ' ' |cut -d" " -f2 |sort -n |uniq -c

 

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

[root@centos7 ~]# grep -e ^s -e ^S/proc/meminfo

[root@centos7 ~]# grep "^[sS]"/proc/meminfo

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

[root@centos7 ~]# grep -v"/bin/bash$" /etc/passwd

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

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

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

[root@centos7 ~]# grep"\<[[:digit:]]\{2,3\}\>" /etc/passwd

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

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

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

[root@centos7 ~]# netstat -tan |grep "\<LISTEN[[:space:]]\+$"

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

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

[root@centos7 ~]# grep "^\([[:alnum:]]\{1,\}\):.*\1$" /etc/pa

[root@centos7 ~]# grep "^\(.*\):.*\1$" /etc/passwdsswd,必須把模式錨定稱為單詞

且后兩者中的:本身就是一個:號字符,結合/etc/passwd中的:號界定了用戶名

 

1 、顯示當前系統root 、mage 或wang 用戶的UID 和默shell

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

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

注意第一條命令中的:號的使用

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

[root@centos7 ~]# egrep "^[[:alpha:]_]+\(\).*"/etc/rc.d/init.d/functions是正確的

[root@centos7 ~]# grep "^[[:alpha:]_]\+[(][)].*"/etc/rc.d/init.d/functions,中括號也可滿足

[root@centos7 ~]# grep "^[[:alpha:]_]\+\(\).*"/etc/rc.d/init.d/functions卻是錯誤的,互為轉義

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

[root@centos7 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o"[[:alnum:]]+$"

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

先使用#echo “/etc/rc.d/init.d/functions”把路徑轉換為文本,然后再作進一步的處理

由于egrep=grep –E,所以grep的選項egrep同于實用

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

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

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

5 、統計以root 身份登錄的每個遠程主機IP地址的登錄次數(用netstat –tn或者who來查看遠程IP)

[root@centos7 ~]# who |egrep -o"\([[:digit:].]+\)" |tr -d '()'|sort -n | uniq -c

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

[0-9]、[1-9][0-9]、[1][0-9]{2}、[2][0-4][0-9]、[2][5][0-5]

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

[root@centos7 ~]# ifconfig| egrep  -o  "IP地址正則表達式"

8、用正則表達式表示IP地址(|是或者的意思)

IP地址的長度為32位,分為4段,每段8位,用十進制數字表示,每段數字范圍為0~255,段與段之間用英文句點“.”隔開,例如:某臺計算機IP地址為10.11.44.100;

分析IP地址的組成特點:250-255、200-249、0-199;
這三種情況可以分開考慮:
1. 250-255:特點:三位數,百位是2,十位是5,個位是0~5,用正則表達式可以寫成:25[0-5]
2. 200-249:特點:三位數,百位是2,十位是0~4,個位是0~9,用正則表達式可以寫成:2[0-4][0-9]
3. 0-199:這個可以繼續分拆,這樣寫起來更加簡單明晰
  3.1. 0-9:特點:一位數,個位是0~9,用正則表達式可以寫成:[0-9]
  3.2. 10-99:特點:二位數,十位是1~9,個位是0~9,用正則表達式可以寫成:[1-9][0-9]
  3.3.100-199:特點:三位數,百位是1,十位是0~9,個位是0~9,用正則表達式可以寫成1[0-9]{2}

[0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]

“(([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])

其中.號進行了轉義

9、用正則表達式表示手機號

“\<1(3|4|5|7|8)[0-9]{9}\>”

10、用正則表達式表示身份證號

“\<((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|(71|81|82))([0-9]){4}(19|20)([0-9]){2}((0[1-9])|(1[0-2]))(0[1-9]|(1[0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>”

11、用正則表達式表示郵箱號

"\<([[:alnum:]]+(-|_)*[[:alnum:]]*)\>@([[:alnum:]]+\.)+[[:alnum:]]+"

 

1 、刪除/etc/grub2.cfg文件中所有以空白開頭的行行首的空白字符

[root@centos7 ~]# sed -n's/^[[:space:]]\+//p' /etc/grub2.cfg

2 、刪除/testdir/fstab文件中所有以#開頭,后面至少跟一個空白字符的行的行首的#和空白字符(sed命令的-n選項是不輸出模式空間所有內容的自動打印,腳本命令p是僅打印模式空間中處理改動的內容,但是sed命令本身就默認輸出模式空間所有內容的自動打印,所有-n選項和腳本命令的同時使用只會打印模式空間中處理改動的內容)

[root@centos7 testdir]# cat fstab | sed -r -n 's/^#[[:space:]]+//p'

3 、在/etc/fstab每一行行首增加#號

[root@centos7 ~]# sed -n 's/^/#&/p'/etc/fstab

4 、在/etc/fstab文件中不以#開頭的行的行首增加#號

[root@centos7 ~]# sed -n 's/^[^#]/#&/p'/etc/fstab

5 、處理/etc/fstab路徑, 使用sed命令取出其目錄名和基名

取出目錄名的兩種方法:

[root@centos7 tmp]# echo"/etc/fst/sd/" | sed -r 's#[^/]+/?$##'

[root@centos7 tmp]# echo"/etc/rc.d/init.d/functions" | sed -r 's@^(.*/)([^/]+/?)$@\1@'

取出基名的兩種方法:

[root@centos7 tmp]# echo"/etc/rc.d/init.d/functions" |sed -r 's@^(.*/)([^/]+/?)$@\2@'

[root@centos7 ~]# echo"/etc/fstab/" |sed 's/.*\<//'

6 、利用sed取出ifconfig命令中本機的IPv4地址

[root@centos7 ~]# ifconfig |sed -n '2p' |sed 's/^.*inet//'|sed 's/n.*//'

7 、統計centos安裝光盤中Package 目錄下的所有rpm文件的以.分隔倒數第二個字段的重復次數

# ls/run/media/root/CentOS\ 7\ x86_64/Packages/ |sed -r's@^(.*\.)(.*)\.rpm$@\2@'|sort|uniq –c

# ls/run/media/root/CentOS\ 7\ x86_64/Packages/ |rev|cut -d. -f2|rev|sort|uniq –c

其中第一種方法中.號要進行轉義

 

 

 

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

(0)
1861276386318612763863
上一篇 2016-08-15 09:24
下一篇 2016-08-15 09:24

相關推薦

  • Nginx配置進階

    目錄 ngx_http_rewrite_module模塊 ngx_http_gzip_module模塊 ngx_http_fastcgi_module模塊 ngx_http_ssl_module模塊 ngx_http_referer_module模塊 ngx_http_rewrite_module模塊 將用戶某一次請求的URI當中的字符串是不是能夠被我們給出…

    Linux干貨 2016-11-05
  • 運維監控大數據的提取與分析

    本文內容整理來自【敏捷運維大講堂】蔣君偉老師的線上直播分享。分別從以下3個維度來分享:1、云時代監控分析的窘境;2、使用標簽標記監控數據的維度;3、監控數據應用場景。 云時代監控分析的窘境 在虛擬化與容器技術廣泛應用的情況下,運維對象大規模地增長,監控平臺每天存儲的指標都以億計,所以監控數據如今已經成了大數據。傳統的監控工具在這種場景下,對于數據的提取分析,…

    系統運維 2017-01-09
  • HAproxy實戰

    HAproxy實驗一 1、實驗要求: (1) 動靜分離discuzx,動靜都要基于負載均衡實現; (2) 進一步測試在haproxy和后端主機之間添加varnish緩存(見實驗二步驟); (3) 給出拓撲設計; (4) haproxy的設定要求: (a) 啟動stats; (b) 自定義403、502和503的錯誤頁; (c) 各組后端主機選擇合適的調度方法…

    Linux干貨 2016-11-15
  • Linux系統命令格式及基礎命令、幫助信息。

    一、Linux系統上的命令使用格式 命令的語法通用格式:                 # COMMAND OPTIONS ARGUMENTS     &nb…

    Linux干貨 2016-10-30
  • Linux進程管理常用命令(二)

       htop命令:         選項:         -d # : 指定延遲時間間隔;         -u  UserName :僅顯示指定用戶的進程;       &n…

    Linux干貨 2017-01-05
  • Linux終端類型

            終端是一種字符型設備,它有多種類型,通常使用tty來簡稱各種類型的終端設備。 [root@localhost ~]# ll /dev|egrep 'tty|pts' crw-rw-rw-. 1&nb…

    Linux干貨 2016-10-14
欧美性久久久久