文本處理練習:
1.找出本機ip地址
[root@localhost ~]# ifconfig |head -2 |tail -1 |tr -s ' ' ':' |cut -d: -f3
10.1.252.221
2.查看本機分區最大的利用率
[root@localhost ~]# df |tr '%' ' ' |tr -s ' ' ':' |cut -d: -f5 |tr '[:alpha:]' ' ' |sort |tail -1
29
3.查出用戶UID最大值的用戶名,UID,及shell類型
[root@localhost ~]# getent passwd |cut -d: -f1,3,7 |sort -t: -k2 -n |tail -1
nfsnobody:65534:/sbin/nologin
4.查出/tmp的權限,以數字方式顯示
[root@localhost ~]# stat /tmp |head -4 |tail -1 |tr -s ' ' ':' |cut -d: -f2 |tr -cd '[:digit:]'
1777
5.統計當前連接本機的每個遠程主機IP的連接數,并按從小到大排序
[root@localhost ~]# netstat -nt |grep 'tcp' |tr -s ' ' ':' |cut -d: -f4 |uniq -c
5 10.1.252.221
正則表達式練習:
1.顯示/proc/meminfo文件中以大小寫s開頭的行
[root@localhost home]# grep -i '^s' /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2098172 kB
SwapFree: 2098172 kB
Shmem: 6992 kB
Slab: 70944 kB
SReclaimable: 41568 kB
SUnreclaim: 29376 kB
[root@localhost home]# grep '^[Ss]' /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2098172 kB
SwapFree: 2098172 kB
Shmem: 6992 kB
Slab: 70944 kB
SReclaimable: 41568 kB
SUnreclaim: 29376 kB
2.顯示/etc/passwd文件中不以/bin/bash結尾的行
[root@localhost ~]# grep -v '.*/bin/bash$' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
……
3.顯示rpc用戶默認的shell程序
[root@localhost ~]# grep '^rpc\>' /etc/passwd |cut -d: -f7
/sbin/nologin
4.找出/etc/passwd中的兩位或三位數
[root@localhost ~]# grep -w '[[:digit:]]\{2,3\}' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
……
5.顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存在非空白字符的行
[root@localhost ~]# grep '^[[:space:]]\+.*[^[:space:]][^[:space:]]*' /etc/grub2.cfg
load_env
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
set default="${saved_entry}"
menuentry_id_option="–id"
menuentry_id_option=""
6.找出“netstat-tan”命令的結果中以“LISTEN”后跟任意多個空白字符結尾的行
[root@localhost ~]# netstat -tan |grep '.*LISTEN[[:space:]]*$'
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
7.添加用戶bash,testbash,basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名的行
[root@localhost ~]# getent passwd |grep '^\<\(.*\)\>.*\b\1\b$'
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1001:1001::/home/bash:/bin/bash
nologin:x:1004:1004::/home/nologin:/sbin/nologin
擴展正則表達式練習:
1、顯示當前系統root、 mage或wang用戶的UID和默認shell
[root@localhost ~]# getent passwd |grep -E '^\<(root|mage|wang)\>' |cut -d: -f3,7
0:/bin/bash
1005:/bin/bash
1006:/bin/bash
2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包
括下劃線)后面跟一個小括號的行
[root@localhost ~]# grep -E '^(_|[[:alpha:]])+\(' /etc/init.d/functions
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@localhost ~]# echo '/etc/rc.d/init.d/functions' |egrep -o '[^/]+/?$'
functions
4、使用egrep取出上面路徑的目錄名(待確定)
[root@localhost ~]# echo '/etc/rc.d/init.d/functions' |egrep -o '(/.*/)'
/etc/rc.d/init.d/
5、統計以root身份登錄的每個遠程主機IP地址的登錄次數
[root@localhost ~]# last |egrep -o '^root\>.*([[:digit:]]\.)[[:digit:]]+' |tr -s ' ' |cut -d" " -f1,3 |sort |uniq -c
15 root 10.1.250.107
1 root 192.168.1.104
6、利用擴展正則表達式分別表示0-9、 10-99、 100-199、200-249、 250-255
[0-9]
[1-9][0-9]
1[0-9]{2}
2[0-4][4-9]
25[0-5]
7、顯示ifconfig命令結果中所有IPv4地址
[root@localhost ~]# 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])'
10.1.252.221
255.255.0.0
10.1.255.255
127.0.0.1
255.0.0.0
原創文章,作者:Mr.Lee,如若轉載,請注明出處:http://www.www58058.com/30041