Linux正則表達式練習


練習一
1、生成30位的隨機口令 [root@centos7 ~]#cat /dev/urandom | tr -dc "[:alnum:]" | head -c30 RJL5qcA5PsQHnYE4kXui0oNkm1FNh1 2、判斷主機版本號 [root@centos7 ~]#grep -o "[0-9]\+" /etc/centos-release | head -n1 練習二 1、找出ifconfig “網卡名” 命令結果中本機的IPv4地址 ifconfig |egrep -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@centos7 ~]#df | grep "/dev/sd" | grep -o "[0-9]*%" | grep -o "[0-9]\+" | sort -n | tail -1 3、查出用戶UID最大值的用戶名、UID及shell類型 [root@centos7 app]#cat /etc/passwd | sort -nr -t: -k3 | head -n1 | cut -d: -f1,3,7 nfsnobody:65534:/sbin/nologin 4、查出/tmp的權限,以數字方式顯示 方法一: [root@centos7 app]#stat -c %a /tmp 1777 方法二: [root@centos7 app]#stat /tmp | grep Uid | cut -d\( -f2 | cut -d/ -f1 1777 方法三 stat /tmp | grep Uid | cut -d\( -f2 | head -c4 5、統計當前連接本機的每個遠程主機IP的連接數,並按從大到小排序 顯示文件/etc/init.d/functions所有方法 方法一 grep ".*{$" /etc/init.d/functions | tr -d { 方法二 [root@centos7 ~]#grep -o "^.*()" /etc/init.d/functions 規范方法三 grep "^[[:alnum:]_]\+[[:space:]]*()" /etc/init.d/functions 練習三 1、顯示/proc/meminfo文件中以大小寫s開頭的行(要求:使用兩種方法,不要理解以s開頭的單詞) grep -i "^s.*" /proc/meminfo 方法一 grep "^[Ss].*" /proc/meminfo 方法二 2、顯示/etc/passwd文件中不以/bin/bash結尾的行 grep -v "/bin/bash$" /etc/passwd 3、顯示用戶rpc默認的shell程序 grep "^rpc\>" /etc/passwd | cut -d: -f7 4、找出/etc/passwd中的兩位或三位數 grep -o "\<[0-9]\{2,3\}\>" /etc/passwd 5、顯示CentOS7的/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行 grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg 6、找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多個空白字符結尾的行 netstat -tan| grep "LISTEN[[:space:]]*$" 7、顯示CentOS7上所有系統用戶的用戶名和UID cut -d: -f1,3 /etc/passwd | grep "\<[[:digit:]]\{,3\}$" "\<[[:digit:]]\{,3\}\>"(123用戶名能匹配) 注意與上正則區別 8、添加用戶bash、testbash、basher、sh、nologin(其shell 為/sbin/nologin),找出/etc/passwd用戶名同shell名的行 grep "^\(.*\):.*\<\1$" /etc/passwd 9、利用df和grep,取出磁盤各分區利用率,並從大到小排序 df | grep "^/dev/sd" | grep -o "[0-9]\{1,3\}%" | grep -o "[0-9]\{1,3\}" | sort -rn 練習四 1、顯示三個用戶root、mage、wang的UID和默認shell [root@centos7 ~]#grep "^\(root\)\|^\(xiaojun\)\|^\(zilong\)" /etc/passwd | cut -d: -f3,7 0:/bin/bash 1001:/bin/bash 1011:/bin/bash 2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下划線)后面跟一個小括號的行 [root@centos7 ~]#grep -o "^[[:alpha:]]\+\>(" /etc/rc.d/init.d/functions checkpid( daemon( killproc( ….. 3、使用egrep取出/etc/rc.d/init.d/functions中其基名 echo "/etc/rc.d/init.d/functions" | egrep -o "[^/]+$" 擴展:取出/etc/rc.d/init.d/基名 [root@centos7 ~]#echo "/etc/rc.d/init.d/" | egrep -o "[^/]+/?$" 4、使用egrep取出/etc/rc.d/init.d/functions路徑的目錄名 [root@centos7 ~]#echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/" | egrep -o "^/.*[^/]" 5、統計last命令中以root登錄的每個主機IP地址登錄次數 last | grep "root" | tr -s " " ":" | cut -d : -f3 | egrep "([0-9]+.){3}[0-9]+" | uniq -c 6、利用擴展正則表達式分別表示 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 ens33 | 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])\>" 192.168.10.150 255.255.255.0 192.168.10.255


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM