正規表示式 練習

2021-10-19 12:21:50 字數 1421 閱讀 8026

1、顯示/proc/meminfo檔案中以大小s開頭的行(要求:使用兩種方法)

grep

"^\(s\|s\)" /proc/meminfo

或:grep

"^[ss].*" /proc/meminfo

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

grep -v "/bin/bash$" /etc/passwd
3、顯示使用者rpc預設的shell程式

grep -w rpc /etc/passwd|

cut -d: -f7

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

cat /etc/passwd |

grep -o '\<[0-9]\\>'

5、顯示centos7的/etc/grub2.cfg檔案中,至少以乙個空白字元開頭的且後面有非空白字元的行

egrep

"^[[:space:]]+.*[^[:space:]]$" /etc/grub2.cfg

6、找出「netstat-tan」命令結果中以listen後跟任意多個空白字元結尾的行

netstat -tan |

grep

"listen[[:blank:]]*"

7、顯示centos7上所有uid小於1000以內的使用者名稱和uid

cut -d: -f1,3 /etc/passwd|

egrep -o ".*:[0-9]"

8、新增使用者bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd使用者名稱和shell同名的行

useradd -s /sbin/nologin bash

useradd -s /sbin/nologin testbash

useradd -s /sbin/nologin basher

useradd -s /sbin/nologin sh

useradd -s /sbin/nologin nologin

grep

"^\([^:]\+\):.*\<\1$" /etc/passwd (基礎正則)

或:egrep

"^([^:]+):.*\<\1$" /etc/passwd (擴充套件正則)

9、利用df和grep,取出磁碟各分割槽利用率,並從大到小排序

df

|grep

"^/dev/sd"

|tr -s " "

|cut -d" " -f5|

sort -nr

正規表示式練習

取出其中的參考文獻,注意到每行只有乙個參考文獻,所以直接用 re.search regex,line import re with open test2 r as f lines f.readlines regex re.compile r a z reg open refer.txt w for ...

正規表示式練習

1 匹配一段文字中的每行的郵箱 y 123 qq.comaaa 163.combbb 126.comasdfasfs33333 adfcom import reret re.findall w qq 163 126 com y print ret 123 qq.com aaa 163.com bbb...

正規表示式練習

字元描述 匹配前面的子表示式零次或多次。例如,zo 能匹配 z 以及 zoo 等價於。匹配前面的子表示式一次或多次。例如,zo 能匹配 zo 以及 zoo 但不能匹配 z 等價於 匹配前面的子表示式零次或一次。例如,do es 可以匹配 do does 中的 does doxy 中的 do 1 va...