正則練習題(包含文本處理練習題)
問題
-
找出ifconfig命令結果中本機的所有IPv4地址
-
查出分區空間使用率的最大百分比值
-
查出用戶UID最大值的用戶名、UID及shell類型
-
查出/tmp的權限,以數字方式顯示
-
統計當前連接本機的每個遠程主機IP的連接數,并按從大 到小排序
答;
-
ifconfig | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
-
df -h | tr -s ' ' ':' | cut -d : -f 5 | cut -d '%' -f1
-
cat /etc/passwd | sort -t : -k3n | cut -d : -f1,3,6 | tail -n 1
-
stat /tmp/ | tr -s ' ' ':' | cut -d : -f 2 | grep ^'(' | grep -o [0-9][0-9][0-9][0-9]或者 stat /tmp |head -4|tail -1 |tr "/" "("|cut -d "(" -f2
-
netstat -nt | grep tcp | tr -s " " ";" | cut -d ";" -f5 | uniq -c | sort -n
問題
-
顯示/proc/meminfo文件中以大小s開頭的行;(要求:使 用兩種方式)
-
顯示/etc/passwd文件中不以/bin/bash結尾的行
-
顯示用戶rpc默認的shell程序
-
找出/etc/passwd中的兩位或三位數
-
顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的 且后面存非空白字符的行
-
找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多 個空白字符結尾的行
-
添加用戶bash、testbash、basher以及nologin(其shell為 /sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名 的行
答:
-
cat /proc/meminfo | grep -i ^s或者 grep ^[sS]
-
cat /etc/passwd | grep -v "/bin/bash"
-
cat /etc/passwd | grep -w rpc | cut -d : -f 7
-
cat /etc/passwd | grep -n '[0-9]\{2,3\}'
-
cat /etc/grub2.cfg | grep "^[[:space:]]\{1,\}.\{1,\}"
-
netstat -tan | grep 'LISTEN[[:space:]]*$'
-
grep -n '^\(\b[[:alnum:]]\{1,\}\b\):.*\1$' /etc/passwd 或者grep -n '^\(\b.*\{1,\}\b\):.*\1$' /etc/passwd
問題
-
顯示三個用戶root、mage、wang的UID和默認shell
-
找出/etc/rc.d/init.d/functions文件中行首為某單詞(包 括下劃線)后面跟一個小括號的行
-
使用egrep取出/etc/rc.d/init.d/functions中其路徑基名
-
使用egrep取出上面路徑的目錄名
-
利用擴展正則表達式分別表示0-9、10-99、100-199、 200-249、250-255
-
顯示ifconfig命令結果中所有IPv4地址
答:
-
cat /etc/passwd | egrep '^\b(root|user1|user2)\b' | cut -d : -f 1,3,7
-
cat /etc/rc.d/init.d/functions | grep -n -w "^.*()" 或者 egrep -n '^(\b(\w{1,})\b)\(\)' /etc/rc.d/init.d/functions
-
echo "/etc/rc.d/init.d/functions" | egrep -o "[^/]+/?$"
-
echo "/etc/rc.d/init.d/functions" | egrep -o '^(/)\b.*\1\b'
-
egrep [0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]
-
ifconfig | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
原創文章,作者:forest,如若轉載,請注明出處:http://www.www58058.com/31322