grep,egrep,正規表示式

2022-04-18 16:14:19 字數 4712 閱讀 7416

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

grep "s|s" /proc/meminfo

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

cat /proc/meminfo | grep -i "^s"

cat /proc/meminfo | grep "[1]

"grep -e ^s -e ^s /proc/meminfo

grep -i "^s" /proc/meminfo

grep "[2]

" /proc/meminfo

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

grep -v "/bin/bash$" /etc/passwd

grep -v ".*$" /etc/passwd

3、顯示使用者rpc預設的shell程式

grep "^rpc:" /etc/passwd|cut -d: -f7

getent passwd rpc |cut -d: -f7

grep "^rpc>" /etc/passwd|cut -d: -f7

grep "^rpc\b" /etc/passwd|cut -d: -f7

grep -w "^rpc" /etc/passwd|cut -d: -f7

grep "^" /etc/passwd|grep -o "/[[:alpha:]]+/[[:alpha:]]+$"

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

grep -o "<[0-9]>" /etc/passwd

grep -o "\b[0-9]\b" /etc/passwd

grep -o "<[[:digit:]]>" /etc/passwd

grep -w "[0-9]" /etc/passwd

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

grep "[[:space:]]+[[:space:]]" /etc/grub2.cfg

grep "[3]

+" /etc/grub2.cfg |grep "[$]"

grep "[4]

+" /etc/grub2.cfg|grep -v "^$"

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

netstat -tan|grep "listen[[:space:]]*$"

7、顯示centos7上所有系統使用者的使用者名稱和uid

cut -d: -f1,3 /etc/passwd|grep "<([1-9][0-9])$"

cut -d: -f1,3 /etc/passwd|grep "<([1-9][0-9]?|[1-9][0-9])$"

cut -d: -f1,3 /etc/passwd|grep "<([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$"

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

grep "^(.>).

<\1$" /etc/passwd

grep "^(.)>.

<\1$" /etc/passwd

grep "^(.):.\b\1$" /etc/passwd

grep "^(.):.

<\1$" /etc/passwd

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

df|grep "/dev/sd"|grep -o "[0-9]%"|grep -o "[0-9]"|sort -rn

df|grep "/dev/sd"|grep -o "<[0-9]>"|sort -nr***不嚴謹,會有bug--<[0-9]>前面的資訊可能會匹配到

df|grep "/dev/sd"|grep -o "[0-9]%"|grep -o "[0-9]"|sort -rn

df|grep "/dev/sd"|tr -s " " "%"|cut -d% -f5|sort -nr

df|grep "/dev/sd"|grep -o "[0-9]%"|cut -d% -f1|sort -nr

df|grep /dev/sd|grep -o "[0-9]+%"|grep -o "[0-9]+"|sort -rn

1、顯示三個使用者root、mage、wang的uid和預設shell

grep -e "^(root|mage|wang)>" /etc/passwd|cut -d: -f1,3,7=grep -e "^(root|mage|wang)\b" /etc/passwd|cut -d: -f1,3,7

cut -d: -f1,7 /etc/passwd|egrep "(root|mage|wang):"

grep -ew "^(root|mage|wang)" /etc/passwd|cut -d: -f1,3,7

grep "^<(root|mage|wang)>" /etc/passwd|cut -d: -f1,7

cut -d: -f1,7 /etc/passwd|grep -e "" -e "" -e ""

2、找出/etc/rc.d/init.d/functions檔案中行首為某單詞(包括下劃線)後面跟乙個小括號的行

cat /etc/rc.d/init.d/functions |grep -o "^<([[:alpha:]])+>()"

cat /etc/rc.d/init.d/functions |egrep "^<([[:alpha:]]+)>()"

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

basename /etc/rc.d/init.d/functions

echo /etc/rc.d/init.d/functions |egrep -o "[^/]+$"(不嚴謹,會有bug--/etc/rc.d/init.d/這個就不能匹配)

echo /etc/rc.d/init.d/functions |egrep -o "[^/]+/?$"

echo /etc/rc.d/init.d/functions|egrep -o "/[[:alpha:]]+$"|egrep -o "[[:alpha:]]+"

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

dirname /etc/rc.d/init.d/functions

echo /etc/rc.d/init.d/functions |egrep -o '^/.*/<'

5、統計last命令中以root登入的每個主機ip位址登入次數

last|egrep "root"|egrep --color=auto -o "([0-9].)[0-9]"|sort|uniq -c

6、利用擴充套件正規表示式分別表示0-9、10-99、100-199、200-249、250-255

0-9:echo |egrep "[0-9]"

10-99:echo |egrep "[1-9][0-9]"

100-199:echo |egrep "1[0-9]"

200-249:echo |egrep "2[0-4][0-9]"

250-255:echo |egrep "25[0-5]"

7、顯示ifconfig命令結果中所有ipv4位址

基礎版:ifconfig ens33|grep -o "([0-9].)[0-9]"

ifconfig ens33|grep -o "[0-9].[0-9].[0-9].[0-9]"

ifconfig ens33|grep -eo "([0-9].)[0-9]"

ifconfig ens33|egrep -o "([0-9].)[0-9]"

擴充套件版:ifconfig|egrep -o "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.)([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>"

全部(包括掩碼等):ifconfig|egrep "(([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|2[0-4][0-9]|25[0-5])\.)([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|2[0-4][0-9]|25[0-5])"

ifconfig|egrep -o "\<(([0-9]|[1-9][0-9]|1[0-9]|2[0-4][0-9]|25[0-5])\.)([0-9]|[1-9][0-9]|1[0-9]|2[0-4][0-9]|25[0-5])\>"

ifconfig|egrep -o "\<(([1-9]?[0-9]|1[0-9]|2[0-4][0-9]|25[0-5])\.)([0-9]|[1-9][0-9]|1[0-9]|2[0-4][0-9]|25[0-5])\>"

8、將此字串:welcome to magedu linux 中的每個字元去重並排序,重複次數多的排到前面

echo welcome to magedu linux|grep -o "."|sort|uniq -c|sort -nr

echo "welcome to magedu linux"|egrep -o "."|sort|uniq -c|sort -r

ss ↩︎

ss ↩︎

[:space:] ↩︎

[:space:] ↩︎

Grep egrep 與正規表示式

grep egrep 與正規表示式 原始出處 作者資訊和本宣告。否則將追究法律責任。首先要記住的是 正規表示式與萬用字元不一樣,它們表示的含義並不相同 正規表示式只是一種表示法,只要工具支援這種表示法,那麼該工具就可以處理正規表示式的字串。vim grep awk sed 都支援正規表示式,也正是因...

linux正規表示式 grep egrep用法

令1 命令2 如果這個命令1執行成功 那麼執行這個命2 mv myfile myfile2 echo if you are seeing this then mv was success 如果 左邊的命令 命令1 未執行成功,那麼就執行 右邊的命令 命令2 mv myfile myfile2 ech...

linux正規表示式 grep egrep用法

令1 命令2 如果這個命令1執行成功 那麼執行這個命2 mv myfile myfile2 echo if you are seeing this then mv was success 如果 左邊的命令 命令1 未執行成功,那麼就執行 右邊的命令 命令2 mv myfile myfile2 ech...