7月26日上課練習(xí)和作業(yè)

1、基本的正則表達(dá)式

  • 顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
[root@centos7 app]#cat /proc/meminfo|grep "^[sS]"
SwapCached:           28 kB
SwapTotal:       2097148 kB
SwapFree:        2097060 kB
Shmem:              7796 kB
Slab:             108060 kB
SReclaimable:      50880 kB
SUnreclaim:        57180 kB
[root@centos7 app]#cat /proc/meminfo|grep -i "^s"
SwapCached:           28 kB
SwapTotal:       2097148 kB
SwapFree:        2097060 kB
Shmem:              7796 kB
Slab:             108060 kB
SReclaimable:      50880 kB
SUnreclaim:        57180 kB
  • 顯示/etc/passwd文件中不以/bin/bash結(jié)尾的行
cat /etc/passwd |grep -v "/bin/bash$"
  • 顯示用戶rpc默認(rèn)的shell程序
[root@centos7 app]#cat /etc/passwd |grep "^rpc\>"|cut -d: -f7
/sbin/nologin
[root@centos7 app]#cat /etc/passwd |grep -w "^rpc"|cut -d: -f7
/sbin/nologin
  • 找出/etc/passwd中的兩位或三位數(shù)
grep -o "\<[0-9]\{2,3\}\>" /etc/passwd
  • 顯示CentOS7的/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
cat /etc/grub2.cfg |grep "^[[:space:]]\+[^[:space:]]"
  • 找出“netstat -tan”命令的結(jié)果中以‘LISTEN’后跟任意多個空白字符結(jié)尾的行
netstat -tan |grep "LISTEN[[:space:]]*$"
  • 顯示CentOS7和centos6上所有系統(tǒng)用戶的用戶名和UID
cat /etc/passwd |cut -d: -f1,3|grep "\<[0-9]\{1,3\}\>"
cat /etc/passwd |cut -d:  -f1,3|grep "\<\([0-9]\{1,2\}\|[1-4][0-9]\{2\}\)\>" 
  • 添加用戶bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶名同shell名的行
grep "^\([[:alnum:]_]\+\>\).*\<\1$" /etc/passwd
  • 利用df和grep,取出磁盤各分區(qū)利用率,并從大到小排序
[root@centos6 app]#df |grep "^/dev/sd"|tr -s " " "%"|cut -d% -f5|sort -nr
10
4
1

2、擴(kuò)展的正則表達(dá)式

  • 顯示三個用戶root、mage、wang的UID和默認(rèn)shell
[root@centos6 app]#cat /etc/passwd |egrep "^(root|zhang|tom)\>"|cut -d: -f1,3,7
root:0:/bin/bash
zhang:500:/bin/bash
tom:504:/bin/bash
  • 找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
egrep  "^[[:alpha:]_]+\(\)" /etc/rc.d/init.d/functions
  • 使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@centos6 app]#echo /etc/rc.d/init.d/functions |egrep -o "[^/]+/?$"
functions
[root@centos6 app]#echo /etc/rc.d/init.d/functions/ |egrep -o "[^/]+/?$"
functions/
  • 使用egrep取出上面路徑的目錄名
[root@centos6 app]#echo /etc/rc.d/init.d/functions/ |egrep -o "^/.*/\<"
/etc/rc.d/init.d/
[root@centos6 app]#echo /etc/rc.d/init.d/functions |egrep -o "^/.*/\<"
/etc/rc.d/init.d/
  • 統(tǒng)計(jì)last命令中以root登錄的每個主機(jī)IP地址登錄次數(shù)
[root@centos6 app]#last |egrep "^root\>"|egrep -o "(\<[0-9]{1,3}\.){3}\<[0-9]{1,3}\>"|sort|uniq -c
     59 192.168.25.1
  • 利用擴(kuò)展正則表達(dá)式分別表示0-9、10-99、100-199、200-249、250-255
echo {0..255}|egrep -o "\<\[0-9]>"
echo {0..255}|egrep -o "\<[1-9][0-9]\>"
echo {0..255}|egrep -o "\<1[0-9]{2}\>"
echo {0..255}|egrep -o "\<2[0-4][0-9]\>"
echo {0..255}|egrep -o "\<25[0-5]\>"
  • 顯示ifconfig命令結(jié)果中所有IPv4地址
ifconfig |egrep -o "\<(([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
  • 將此字符串:welcome to magedu linux 中的每個字符去重并排序,重復(fù)次數(shù)多的排到前面
echo "welcome to magedu linux" |egrep -o [[:alpha:]] |sort|uniq -c
  • 將/misc/cd/Packages 文件中以.rpm結(jié)尾的文件,cpu架構(gòu)取出來,并統(tǒng)計(jì)每種架構(gòu)出現(xiàn)的次數(shù)
[root@centos6 Packages]#ls *.rpm |egrep -o "[^.]*\.rpm$"|cut -d "." -f1|sort|uniq -c
      4 i686
    925 noarch
   2311 x86_64
[root@centos6 Packages]#ls *.rpm |rev|cut -d "." -f2|rev|sort|uniq -c
      4 i686
    925 noarch
   2311 x86_64
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯誤還是無法避免 以后資料會慢慢更新 大...
    數(shù)據(jù)革命閱讀 13,251評論 2 33
  • Ubuntu的發(fā)音 Ubuntu,源于非洲祖魯人和科薩人的語言,發(fā)作 oo-boon-too 的音。了解發(fā)音是有意...
    螢火蟲de夢閱讀 100,704評論 9 468
  • 201705231、顯示/var目錄下所有以l開頭,以一個小寫字母結(jié)尾,且中間出現(xiàn)至少一位數(shù)字的文件或目錄ls 1...
    JevonWei閱讀 844評論 0 1
  • 本文筆記源自這里——[實(shí)驗(yàn)樓]歡迎大家在下面交流其中有問題的地方喜歡請點(diǎn)收藏,每日更新(全部已親自實(shí)踐). 一. ...
    東皇Amrzs閱讀 4,330評論 7 54
  • awk命令的基本使用 [root@shellscript ~]# head -n 3 /etc/passwd | ...
    古寒飛閱讀 1,107評論 0 2

友情鏈接更多精彩內(nèi)容