1 、找出ifconfig 命令結果中本機的所有IPv4地址
[root@centos7 ~]# ifconfig |head -2 |tail-1 |cut -dn -f2 |cut -d" " -f2
2 、查出分區空間使用率的最大百分比值
[root@centos7 ~]# df |cut -c44-46 |sort -n|tail -2 |head -1
3 、查出用戶UID 最大值的用戶名、UID 及shell 類型
[root@centos7 ~]# cat /etc/passwd |sort -t:-k 3 -n |tail -1 |cut -d: -f1,3,7
4 、查出/tmp 的權限,以數字方式顯示
[root@centos7 ~]# stat /tmp |head -4 |tail-1 |cut -d/ -f1 |cut -d\( -f2
5 、統計當前連接本機的每個遠程主機IP 的連接數,并按從大到小排序
# netstat -tn |cut -d: -f2|tr -s ' ' |cut -d" " -f2 |sort -n |uniq -c
1 、顯示/proc/meminfo 文件中以大小s 開頭的行;( 要求:使用兩種方式)
[root@centos7 ~]# grep -e ^s -e ^S/proc/meminfo
[root@centos7 ~]# grep "^[sS]"/proc/meminfo
2 、顯示/etc/passwd 文件中不以/bin/bash結尾的行
[root@centos7 ~]# grep -v"/bin/bash$" /etc/passwd
3 、顯示用戶rpc 默認的shell程序
[root@centos7 ~]# grep"^rpc\>" /etc/passwd |cut -d: -f7
4 、找出/etc/passwd 中的兩位或三位數
[root@centos7 ~]# grep"\<[[:digit:]]\{2,3\}\>" /etc/passwd
5 、顯示/etc/grub2.cfg 文件中,至少以一個空白字符開頭的且后面跟非空白字符的行
[root@centos7 ~]# grep"^[[:space:]]\+[^[:space:]].*" /etc/grub2.cfg
6 、找出"netstat-tan" 命令的結果中以'LISTEN' 后跟多于0個空白字符結尾的行
[root@centos7 ~]# netstat -tan |grep "\<LISTEN[[:space:]]\+$"
7 、添加用戶bash 、testbash 、basher 以及nologin( 其shell為 為/sbin/nologin), 而后找出/etc/passwd文件中用戶名同shell名的行
[root@centos7 ~]# grep "^\(\<.*\>\).*\1$" /etc/passwd
[root@centos7 ~]# grep "^\([[:alnum:]]\{1,\}\):.*\1$" /etc/pa
[root@centos7 ~]# grep "^\(.*\):.*\1$" /etc/passwdsswd,必須把模式錨定稱為單詞
且后兩者中的:本身就是一個:號字符,結合/etc/passwd中的:號界定了用戶名
1 、顯示當前系統root 、mage 或wang 用戶的UID 和默shell
[root@centos7 ~]# egrep"^(root|mage|wang):" /etc/passwd |cut -d: -f1,3,7
[root@centos7 ~]# egrep"^(root|mage|wang)\>" /etc/passwd |cut -d: -f1,3,7
注意第一條命令中的:號的使用
2 、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
[root@centos7 ~]# egrep "^[[:alpha:]_]+\(\).*"/etc/rc.d/init.d/functions是正確的
[root@centos7 ~]# grep "^[[:alpha:]_]\+[(][)].*"/etc/rc.d/init.d/functions,中括號也可滿足
[root@centos7 ~]# grep "^[[:alpha:]_]\+\(\).*"/etc/rc.d/init.d/functions卻是錯誤的,互為轉義
3 、使用egrep 取出/etc/rc.d/init.d/functions中其基名
[root@centos7 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o"[[:alnum:]]+$"
[root@centos7 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o"[^/]+$"
先使用#echo “/etc/rc.d/init.d/functions”把路徑轉換為文本,然后再作進一步的處理
由于egrep=grep –E,所以grep的選項egrep同于實用
4 、使用egrep取出上面路徑的目錄名
[root@centos7 ~]# echo"/etc/rc.d/init.d/functions" |egrep -o "^/.*/"
[root@centos7 ~]# echo"/etc/rc.d/init.d/functions" |egrep -o "^.*/"
5 、統計以root 身份登錄的每個遠程主機IP地址的登錄次數(用netstat –tn或者who來查看遠程IP)
[root@centos7 ~]# who |egrep -o"\([[:digit:].]+\)" |tr -d '()'|sort -n | uniq -c
6 、利用擴展正則表達式分別表示0-9 、10-99 、100-199、200-249 、250-255
[0-9]、[1-9][0-9]、[1][0-9]{2}、[2][0-4][0-9]、[2][5][0-5]
7 、顯示ifconfig 命令結果中所有IPv4 地址
[root@centos7 ~]# ifconfig| egrep -o "IP地址正則表達式"
8、用正則表達式表示IP地址(|是或者的意思)
IP地址的長度為32位,分為4段,每段8位,用十進制數字表示,每段數字范圍為0~255,段與段之間用英文句點“.”隔開,例如:某臺計算機IP地址為10.11.44.100;
分析IP地址的組成特點:250-255、200-249、0-199;
這三種情況可以分開考慮:
1. 250-255:特點:三位數,百位是2,十位是5,個位是0~5,用正則表達式可以寫成:25[0-5]
2. 200-249:特點:三位數,百位是2,十位是0~4,個位是0~9,用正則表達式可以寫成:2[0-4][0-9]
3. 0-199:這個可以繼續分拆,這樣寫起來更加簡單明晰
3.1. 0-9:特點:一位數,個位是0~9,用正則表達式可以寫成:[0-9]
3.2. 10-99:特點:二位數,十位是1~9,個位是0~9,用正則表達式可以寫成:[1-9][0-9]
3.3.100-199:特點:三位數,百位是1,十位是0~9,個位是0~9,用正則表達式可以寫成1[0-9]{2}
[0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]
“(([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])”
其中.號進行了轉義
9、用正則表達式表示手機號
“\<1(3|4|5|7|8)[0-9]{9}\>”
10、用正則表達式表示身份證號
“\<((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|(71|81|82))([0-9]){4}(19|20)([0-9]){2}((0[1-9])|(1[0-2]))(0[1-9]|(1[0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>”
11、用正則表達式表示郵箱號
"\<([[:alnum:]]+(-|_)*[[:alnum:]]*)\>@([[:alnum:]]+\.)+[[:alnum:]]+"
1 、刪除/etc/grub2.cfg文件中所有以空白開頭的行行首的空白字符
[root@centos7 ~]# sed -n's/^[[:space:]]\+//p' /etc/grub2.cfg
2 、刪除/testdir/fstab文件中所有以#開頭,后面至少跟一個空白字符的行的行首的#和空白字符(sed命令的-n選項是不輸出模式空間所有內容的自動打印,腳本命令p是僅打印模式空間中處理改動的內容,但是sed命令本身就默認輸出模式空間所有內容的自動打印,所有-n選項和腳本命令的同時使用只會打印模式空間中處理改動的內容)
[root@centos7 testdir]# cat fstab | sed -r -n 's/^#[[:space:]]+//p'
3 、在/etc/fstab每一行行首增加#號
[root@centos7 ~]# sed -n 's/^/#&/p'/etc/fstab
4 、在/etc/fstab文件中不以#開頭的行的行首增加#號
[root@centos7 ~]# sed -n 's/^[^#]/#&/p'/etc/fstab
5 、處理/etc/fstab路徑, 使用sed命令取出其目錄名和基名
取出目錄名的兩種方法:
[root@centos7 tmp]# echo"/etc/fst/sd/" | sed -r 's#[^/]+/?$##'
[root@centos7 tmp]# echo"/etc/rc.d/init.d/functions" | sed -r 's@^(.*/)([^/]+/?)$@\1@'
取出基名的兩種方法:
[root@centos7 tmp]# echo"/etc/rc.d/init.d/functions" |sed -r 's@^(.*/)([^/]+/?)$@\2@'
[root@centos7 ~]# echo"/etc/fstab/" |sed 's/.*\<//'
6 、利用sed取出ifconfig命令中本機的IPv4地址
[root@centos7 ~]# ifconfig |sed -n '2p' |sed 's/^.*inet//'|sed 's/n.*//'
7 、統計centos安裝光盤中Package 目錄下的所有rpm文件的以.分隔倒數第二個字段的重復次數
# ls/run/media/root/CentOS\ 7\ x86_64/Packages/ |sed -r's@^(.*\.)(.*)\.rpm$@\2@'|sort|uniq –c
# ls/run/media/root/CentOS\ 7\ x86_64/Packages/ |rev|cut -d. -f2|rev|sort|uniq –c
其中第一種方法中.號要進行轉義
原創文章,作者:18612763863,如若轉載,請注明出處:http://www.www58058.com/31833