shell三剑客练习 05

Posted FikL-09-19

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell三剑客练习 05相关的知识,希望对你有一定的参考价值。

grep

# 1、^ 行首
[root@mm ~]#
grep '^root' /etc/passwd
awk -F: '/^root/' passwd.txt 

# 2、$ 行尾
[root@openvpn grep]# grep 'bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
www:x:666:666::/home/www:/bin/bash 

# 3、. 除了换行符以外的任意单个字符
[root@openvpn grep]#grep 'r..t' /etc/passwd
[root@centos ~]# awk -F: '/r..t/' passwd.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

# 4、* 前导字符的零个或多个
[root@mm ~]# cat a.txt 
a
ab
abb
abbb
bbbbb
[root@mm ~]# grep 'ab*' a.txt 
a
ab
abb
abbb

# 5、.* 所有字符=贪婪
[root@mm ~]# cat a.txt 
a123+-*/c11113333c
a1c
a77Ac
a23333c
ac
111
222
333
[root@mm ~]# grep 'a.*c' a.txt 
a123+-*/c11113333c
a1c
a77Ac
a23333c
ac

# 5.1 .*?=》非贪婪,默认情况下,grep不支持非贪婪修饰符,但您可以使用grep -P来使用Perl语法来支持.*?
[root@openvpn grep]# cat ping.sh 
<a  href="https://www.jd.vom">"我是京东"</a>
<a  href="https://www.taobao.vom">"我是淘宝"</a>

[root@openvpn grep]# grep 'href=".*"' ping.sh  #贪婪
<a  href="https://www.jd.vom">"我是京东"</a>
<a  href="https://www.taobao.vom">"我是淘宝"</a>

[root@openvpn grep]# grep -P 'href=".*?"' ping.sh  #非贪婪
<a  href="https://www.jd.vom">"我是京东"</a>
<a  href="https://www.taobao.vom">"我是淘宝"</a>


[root@openvpn grep]# grep -oP 'href=".*?"' ping.sh # 正则 
href="https://www.jd.vom"
href="https://www.taobao.vom" 

# 6、[] 字符组内的任一字符

# 7、[^] 对字符组内的每个字符取反(不匹配字符组内的每个字符)
[root@mm ~]# cat a.txt 
a1c
a2c
a33c
aAc
aZc
[root@mm ~]# grep 'a[0-9]c' a.txt
a1c
a2c
[root@mm ~]# grep 'a[^0-9]c' a.txt
aAc
aZc
[root@mm ~]# 
[root@mm ~]# grep 'a[0-9][0-9]c' a.txt
a33c
[root@mm ~]# 

# 8、^[^] 非字符组内的字符开头的行
[root@openvpn grep]# cat a.tt
a1b
as2b
a9b
asb
ba
aad
dad
[root@openvpn grep]# egrep '^[^#\\ ]' /etc/ansible/ansible.cfg  以#号开头的行 #\\后面是空格
[defaults]
command_warnings=False
host_key_checking = False
log_path = /var/log/ansible.log
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
[root@openvpn grep]# grep '^[ab]' a.tt #以a或者b开头的
a1b
as2b
a9b
asb
ba
aad
dad
[root@openvpn grep]# grep '^[^ab]' a.tt # 不以a或b开头的
dad

# 9、[a-z] 小写字母
# 10、[A-Z] 大写字母
# 11、[a-Z] 小写和大写字母
# 12、[0-9] 数字	
# 正则
[root@openvpn grep]# cat a.tt
a1b
asb
a+b
a-zb
a2
as2b
a9b
asb
[root@openvpn grep]# grep 'a[-+*/]b' a.tt #减号在前或者在后
a+b
[root@openvpn grep]# grep 'a[^-+*/]b' a.tt #不包含里面的加减乘除符合
a1b
asb
a9b
asb

[root@openvpn grep]# cat a.tt
a1b
asb
a+b
a-zb
a2
as2b
a9b
asb

[root@openvpn grep]# grep 'a[0-9]b' a.tt
a1b
a9b

# 13、\\< 单词头 单词一般以空格或特殊字符做分隔,连续的字符串被当做单词
# 14、\\> 单词尾
[root@mm ~]# netstat -an |grep -w 80
tcp6       0      0 :::80                   :::*                    LISTEN    
[root@mm ~]# netstat -an |grep  '\\<80\\>'
tcp6       0      0 :::80                   :::*                    LISTEN    
[root@mm ~]# netstat -an |grep  '\\b80\\b'
tcp6       0      0 :::80                   :::*                    LISTEN   
[root@mm ~]# echo -e "a\\nb" |grep $'a\\nb'
a
b
[root@mm ~]# 
[root@mm ~]# echo -e "a\\tb" |grep $'a\\tb'
a	b
目标文件/etc/passwd,使用grep命令或egrep
1.显示出所有含有root的行:
[root@openvpn grep]# grep 'root' passwd.txt  |wc -l
2
2.输出任何包含bash的所有行,还要输出紧接着这行的上下各两行的内容:
[root@openvpn grep]# egrep  -C 2 'bash' /etc/passw
3.  显示出有多少行含有nologin。
[root@openvpn grep]# grep  'nologin' /etc/passwd |wc -l
24
4.显示出那些行含有root,并将行号一块输出。
[root@openvpn grep]# grep  -n 'root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
5.显示出文件中
[root@openvpn grep]# grep  -n 'root' /etc/passwd > passwd.txt 
[root@openvpn grep]# cat passwd.txt 
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
6.新建用户
    abominable
    abominate
    anomie
    atomize
    编写正则表达式,将他们匹配出来
[root@openvpn grep]# egrep 'a.omi(nabl|nat|z|)e' /etc/passwd
7.建四个用户
    Alex213sb
    Wpq2222b
    yH438PIG
    mm666
    mm

    过滤出用户名组成是字母+数字+字母的行
[root@openvpn grep]# egrep '[a-Z]+[0-9]+[a-Z]+' /etc/passwd
8.显示出/etc目录下所有包含root的文件名
[root@openvpn grep]# egrep -rl 'root' /etc/
9. 过滤掉/etc/ssh/sshd_config内所有注释和所有空行
[root@openvpn grep]# egrep -v '^#|^$'  /etc/ssh/sshd_config
[root@openvpn sed]# egrep  '^[^#]' /etc/ssh/sshd_config


##### 1、显示/proc/meminfo文件中以不区分大小的s开头的行
[root@m01 ~]# grep -i '^s' /proc/meminfo
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:              7792 kB
Slab:              57176 kB
SReclaimable:      28856 kB
SUnreclaim:        28320 kB
##### 2、显示/etc/passwd中以nologin结尾的行
[root@m01 ~]# grep -E 'nologin$' /etc/passwd
##### 3、显示/etc/inittab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意字符的行
[root@openvpn grep]# egrep '^#\\ +.*' /etc/inittab 
# inittab is no longer used when using systemd.
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
# To view current default target, run:
# systemctl get-default
# To set a default target, run:
# systemctl set-default TARGET.target

##### 4、显示/etc/inittab中包含了:一个数字:(即两个冒号中间一个数字)的行
[root@openvpn grep]# egrep ':[0-9]:' /etc/inittab 

##### 5、显示/boot/grub/grub.conf文件中以一个或多个空白字符开头的行
[root@openvpn grep]# egrep  '^\\ +' /boot/grub/grub.con

##### 6、显示/etc/inittab文件中以一个数字开头并以一个与开头数字相同的数字结尾的行

[root@openvpn grep]# cat 1.txt 
222kkmkkm4354
2334mmm32435452
mm11
3245354mm
34mmmm3456
[root@openvpn grep]# egrep '(^[0-9]{1}).*\\1$'  1.txt 
2334mmm32435452
​```

##### 7、ip a命令可以显示当前主机的IP地址相关的信息等,要求不包括127.0.0.1
[root@openvpn grep]#  ip a | grep -oE "([0-9]{1,3}\\.){3}[0-9]{1,3}" | grep -v "127"

##### 8、显示/etc/sysconfig/network-scripts/ifcfg-eth0文件中的包含了类似IP地址点分十进制数字格式的行
[root@openvpn grep]# egrep -o '([0-9]{1,3}\\.){3}[0-9]{1,3}' /etc/sysconfig/network-scripts/ifcfg-eth0
192.168.15.125
192.168.15.2
223.5.5.5

sed

[root@openvpn ~]# sed -rn '#mm#p' 1.sed
[root@openvpn ~]# sed -r '\\#/etc/passwd#p' 1.sed   #必须加个\\
/etc/passwd
/etc/passwd
passwd
abcd
111
asd
ss
123
/etc/
###### 1、如果c是左斜杠,不需要转义也可以
[root@openvpn ~]# sed -rn '\\/mm/p' test.txt 
2222222mm
333333mm
444444mm
[root@openvpn ~]# sed -rn '/mm/p' test.txt 
2222222mm
333333mm
444444mm
[root@openvpn ~]# 
###### 2、如果匹配的正则里有左斜杠,要么将正则转义,要么将c转义
[root@openvpn ~]# cat a.txt 
/etc/mm/666
etc
[root@openvpn ~]# sed -rn '//etc/mm/666/p' a.txt # 错误
sed: -e expression #1, char 0: no previous regular expression
    
[root@openvpn ~]# sed -rn '/\\/etc\\/mm\\/666/p' a.txt  # 正则转义
/etc/mm/666

[root@openvpn ~]# sed -rn '#/etc/mm/666#p' a.txt # 转义c,必须是\\c
[root@openvpn ~]# sed -rn '\\#/etc/mm/666#p' a.txt  # 转义c
/etc/mm/666
[root@openvpn ~]# 


# 示例
[root@openvpn ~]# cat a.txt 
/etc/mm/666
etc
[root@openvpn ~]# sed -ri '/\\/etc\\/mm\\/666/s/.*/xxx/' a.txt 
[root@openvpn ~]# cat a.txt 
xxx
etc
[root@openvpn ~]# 
打印命令:p
# sed -r "/mm/p" a.txt		
# sed -r -n "/mm/p" a.txt

删除命令:d,注意用单引号
# sed -r '3d' a.txt
# sed -r '3,$d' a.txt
# sed -r '$d' a.txt
# sed -r '/mm/d' a.txt 	
# sed -r '1,/mm/{/mm/d}' a.txt 			# 只删除模式匹配成功的第一行


[root@mm ~]# cat a.txt 
mm111111
mm222222
333mm333
444444mm
5555555555
6666666666
mm777777
8888888888
[root@mm ~]# sed -r '/mm/d' a.txt  # 只删除模式匹配成功的所有行
mm111111
333mm333
5555555555
6666666666
8888888888
[root@mm ~]# sed -r '1,/mm/{/mm/d}' a.txt  # 只删除模式匹配成功的第一行
mm111111
333mm333
444444mm
5555555555
6666666666
mm777777
8888888888


替换命令:s
# sed -r 's/mm/Bigmm/' a.txt 
# sed -r 's/mm/Bigmm/g' a.txt 
# sed -r 's/^mm/Bigmm/g' a.txt
# sed -r -n 's/root/mm/gip' /etc/passwd
# sed -r 's/[0-9]$/&.change/' a.txt		# &代表取到匹配成功的整行内容

[root@openvpn sed]# cat a.tt 
123:456
[root@openvpn sed]# cat mm.sed 
1111111
2222222mm
333333mm
444444mm
555555eon
[root@openvpn sed]# sed -r '/2/r a.tt' mm.sed  #第二行后添加a.tt
1111111
2222222mm
123:456
333333mm
444444mm
555555eon
[root@openvpn sed]# sed -r '3r a.tt' mm.sed  #第三行后添加a.tt
1111111
2222222mm
333333mm
123:456
444444mm
555555eon
# sed -r 's/^([a-zA-Z]+)([^[a-zA-Z]+)/\\2\\1/' a.txt
# sed -r 's#mm#bigmm#g' a.txt	

[root@openvpn sed]# cat c.tt  #例题
123:456
[root@openvpn sed]# sed -r 's/([0-9]+):([0-9]+)/\\2:\\1/' c.tt 
456:123
多重编辑命令:e
# sed -r -e '1,3d' -e 's/[Ee]gon/mm/g' a.txt  # 在前一个-e的基础之上进行第二个-e操作
# sed -r '1,3d;s/[Ee]gon/mm/g' a.txt

# sed -r '3{s/[0-9]/x/g;s/[Ee]gon/mm/g}' a.txt  # 只处理第三行
# sed -r '1,3{s/[0-9]/x/g;s/[Ee]gon/mm/g}' a.txt  # 处理1到3行

# sed -r -n '1p;p' a.txt  # ;分隔依次运行,先针对第一行进行p操作,再针对所有行进行p操作
# sed -r -n '1{p;p}' a.txt  # 只针对第一行,连续进行两次p操作

反向选择!
# sed -r '3d' a.txt
# sed -r '3!d' a.txt


读文件命令:r
# sed -r '/^mm/r b.txt' a.txt  # 在匹配成功的行后添加文件b.txt的内容
# sed -r '/2/r b.txt' a.txt  # 在第2行后面添加文件b.txt的内容
# sed -r ‘3r /etc/passwd’ a.txt #在第三行后添加 /etc/passwd文件
写文件命令:w
# sed -r '/[Ee]gon/w b.txt' a.txt  # 将匹配成功的行写入新文件b.txt		
# sed -r '3,$w /root/new.txt' a.txt # 将第3行到最后一行写入/root/new.txt

追加命令:a
# sed -r '3aXXXXXXXXX' a.txt  # 在第3行后添加一行
# sed -r '3a1111111111111\\               # 可以用\\续行
> 222222222222\\
> 333333333333' a.txt

插入命令:i
# sed -r '2i1111111111111' /etc/hosts  #第2行前添加一行
# sed -r '2i111111111\\
> 2222222222\\
> 3333333333' a.txt

修改命令:c
# sed -r '2c1111111111111' a.txt
# sed -r '2c111111111111\\
> 22222222222\\
> 33333333333' a.txt

把下一行内容读入模式空间:n
# sed -r '/^mm/{n;s/[0-9]/x/g}' a.txt  # 将匹配/^mm/成功的行的下一行读入模式空间进行s处理
[root@openvpn ~]# cat a.txt 
/etc/mm/666
etc
[root@openvpn ~]# sed -r '\\#/etc/mm/666#n;c 1111' a.txt 
/etc/mm/666
1111
[root@openvpn ~]# 

转换命令:y
# sed -r '1,3y/Eeo/12X/' a.txt  # 1到3行进行转换 对应规则:a->1 e->2 o->X

退出:q
# sed -r '5q' a.txt 			
# sed -r '/[Ee]gon/{ s/[0-9]/X/; q; }' a.txt  # 匹配成功/[Ee]gon/则执行{}内命令,q代表退出,即替换一次则退出,如果文件中多行符合规则的内容也只替换了第一个

删除配置文件中用井号#注释的行
sed -r -i '/^#|^$/d' file.conf 
sed -r -i '/^[ \\t]*#/d' file.conf

删除配置文件中用双斜杠//注释的行 
sed -r '\\#//#d' file.conf
sed -r -i '\\c//cd' file.conf

删除无内容空行 
sed -r '/^$/d' file.conf 
sed -r '/^[\\t]*$/d' file.conf 
sed -r '/^[ \\t]*$/d' file.conf


示例:
# 删除#号注释和无内容的空行
sed -r -i '/^[ \\t]*#/d; /^[ \\t]*$/d' /etc/vsftpd/vsftpd.conf
sed -r -i '/^[ \\t]*#|^[ \\t]*$/d' /etc/vsftpd/vsftpd.conf # 同上

追加一行,\\可有可无,有更清晰
sed -r -i '$a\\chroot_local_user=YES' /etc/vsftpd/vsftpd.conf 


给文件每行加注释
sed -r -i 's/^/#/' filename
sed -r '/(^#|^$)/d;s/(.*)/#&/g' inittab

每指定行加注释
sed -r -i '10,$s/^/#/' filename
sed -r '3,$s/^#*/#/' filename		# 将行首连续的零个或多个#换成一个#


sed中使用外部变量
# var1=666
# sed -r 3a$var1 test.txt    # 可以不加引号
# sed -r "3a$var1" test.txt  # 也可以加引号,但注意是双引号而不是单引号,因为要用$符号取变量值
# sed -r '3a'"$var1" test.txt # 也可以sed命令用''引起来,而变量用"",注意二者之间不能有空格

awk

# awk -F: '{if($3>300) {print $0}}' /etc/passwd
# awk -F: '{if($3>300) {print $3} else{print $1}}' /etc/passwd

# awk -F: '{if($3>300) {max=$3;print max} else{max=$1;print max}}' /etc/passwd
# awk -F: '{max=($3>300) ? $3 : $1; print max}' /etc/passwd

# awk -F: '{if($3>$4) {max=$3;print max} else{max=$4; print max}}' /etc/passwd
# awk -F: '{max=($3 > $4) ? $3: $4; print max}' /etc/passwd	
相当于:
if ($3 > $4)
	max=$3
else
	max=$4
	
	+ - * / %(模) ^(幂2^3)
可以在模式中执行计算,awk都将按浮点数方式执行算术运算

# awk -F: '$3 * 10 > 500' /etc/passwd

# 例题
# 月薪*12 大于100000工资
[root@openvpn awk]# cat 1.awk 
mm:16000:25
ping:12000:21
tom:8000:31
[root@openvpn awk]# awk -F: '$2*12 >100000{print $1}' 1.awk 
mm
ping

# 取奇数行
[root@openvpn awk]# cat passwd.txt 
root:x:0:0:root:/root:/bin/bash
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
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@openvpn awk]# awk -F: 'NR % 2 != 0{print NR,$1}' passwd.txt 
1 root
3 daemon
5 lp
7 shutdown
9 mail

# 指定输出
[root@openvpn awk]# cat 1.awk 
mm:16000:25
ping:12000:21
tom:8000:31
[root@openvpn awk]# awk -F: '{print NR,$1}' 1.awk 
1 mm
2 ping
3 tom
[root@openvpn awk]# awk -F: 'BEGIN{OFS="-----"}{print NR,$1}' 1.awk 
1-----mm
2-----ping
3-----tom
# 指定输出内容格式
[root@openvpn awk]# cat 1.awk 
mm:16000:25
ping:12000:21
tom:8000:31
[root@openvpn awk]# awk -F: 'BEGIN{OFS="---"}{printf "行号:%s---用户名:%s\\n",NR,$1}' 1.awk 
行号:1---用户名:mm
行号:2---用户名:ping
行号:3---用户名:tom
[root@openvpn awk]# awk -F: 'BEGIN{OFS="---"}{print "行号:"NR,"用户名:"$1}' 1.awk 
行号:1---用户名:mm
行号:2---用户名:ping
行号:3---用户名:tom

&&			逻辑与		a&&b
||			逻辑或		a||b
!			逻辑非		!a

示例:
# awk '$2 > 5 && $2 <= 15' filename
# awk '$3 == 100 || $4 > 50' filename
# awk '!($2 < 100 && $3 < 20)' filename	

[root@openvpn awk]# free -m | awk '/^Mem/{a=$4/$2*100;print a "%"}' #内存大小
75.7202%
[root@openvpn awk]# df -h | awk '/\\/$/{aa=$4/$2*100; print aa "%"}' #硬盘内存 第一种
73.7374%
[root@openvpn awk]# df | awk '/\\/$/ {print $4/$2*100 "%"}' #硬盘内存 第二种
73.7059%

[root@openvpn ~]# awk '/root/,/egon/{print NR,$0}' a.txt
1 1111root
2 2222root22222
3 egon123123123123
7 root7777
8 1asf
9 asdfasdf
10 egon
[root@openvpn ~]# 

# 行号
awk -F: 'NR>=1 && NR <=3{print $1}' test.txt 

正则练习

1、打印第5行和第10行
[root@centos shell]# awk -F: 'NR==5||NR==10{print $0}' /etc/passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
2、当前系统默认挂载有几个硬盘分区
[root@centos shell]# awk '/xfs/{print $4}' /etc/fstab |wc -l
3、打印php.ini中每一个单词有多少个,并格式化输出
egrep -ow '[a-zA-Z0-9_]+' /etc/php.ini |awk '{num[$1]++}END{for (i in num){printf "|词名:|%-25s|个数|%-5s|\\n",i,num[i]}}'

4、打印a.sh文件的数字权限
ls -l a.sh | awk '{print $1}' | awk  'BEGIN{FS="";u=0;g=0;o=0}{for(i=2;i<=10;i++){if(i<=4){if($i=="r"){u+=4}else if($i=="w"){u+=2}else if($i=="x"){u+=1}else{}}else if(i>4 && i<=7){if($i=="r"){g+=4}else if($i=="w"){g+=2}else if($i=="x"){g+=1}else{}}else{if($i=="r"){o+=4}else if($i=="w"){o+=2}else if($i=="x"){o+=1}else{}}}}END{print u,g,o}'
5、打印当前系统中正在监控的端口
[root@centos shell]# netstat -lntpu|awk '{if(NR>2){print $4}}'|awk -F: '{print $NF}' |uniq -c
      1 9000
      1 6379
      1 80
      1 22
      1 3306
      1 22
      1 68
      4 123

正则练习

1、显示/proc/meminfo文件中以不区分大小的s开头的行
[root@centos shell]# grep -i '^s' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:             21840 kB
Slab:             117880 kB
SReclaimable:     102456 kB
SUnreclaim:        15424 kB
2、显示/etc/passwd中以nologin结尾的行
[root@centos shell]# awk -F: '/nologin$/' /etc/passwd
3、显示/etc/inittab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意字符的行
[root@centos shell]# egrep '^#\\ +.*' /etc/inittab 
# inittab is no longer used when using systemd.
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
# To view current default target, run:
# systemctl get-default
# To set a default target, run:
# systemctl set-default TARGET.target
4、显示/etc/inittab中包含了:一个数字:(即两个冒号中间一个数字)的行
[root@centos shell]# egrep ':[0-9]:' /etc/inittab 

5、显示/boot/grub/grub.
[root@openvpn grep]# egrep  '^\\ +' /boot/grub/grub.con
6、显示/etc/inittab文件中以一个数字开头并以一个与开头数字相同的数字结尾的行
[root@centos shell]# egrep  '(^[0-9]{1}).*\\1$' a.tt 
2334mmm32435452
7、ip a命令可以显示当前主机的IP地址相关的信息等,要求不包括127.0.0.1
[root@centos shell]# ip a |egrep  -o "([0-9]{1,3}\\.){3}[0-9]{1,3}" |grep -v 127
10.0.4.10
10.0.7.255
8、显示/etc/sysconfig/network-scripts/ifcfg-eth0文件中的包含了类似IP地址点分十进制数字格式的行
[root@openvpn grep]# egrep -o '([0-9]{1,3}\\.){3}[0-9]{1,3}' /etc/sysconfig/network-scripts/ifcfg-eth0
192.168.15.125
192.168.15.2
223.5.5.5

9、删除配置文件中用井号#注释的行
[root@centos shell]# egrep '^[^#\\ ]' /etc/ansible/ansible.cfg 
[root@centos shell]# sed -r '/^#|^$/d' /etc/ansible/ansible.cfg 
10. 删除配置文件中用双斜杠//注释的行
sed -r '\\#//#d' file.conf
11. 删除无内容空行 
sed -r '/^[\\t]*$/d' file.conf 
sed -r '/^[ \\t]*$/d' file.conf
12. 删除#号注释和无内容的空行
sed -r -i '/^[ \\t]*#/d; /^[ \\t]*$/d' /etc/vsftpd/vsftpd.conf
sed -r -i '/^[ \\t]*#|^[ \\t]*$/d' /etc/vsftpd/vsftpd.conf # 同上
13. 追加一行,\\可有可无,有更清晰
sed -r '$a\\chroot_local_user=YES' /etc/vsftpd/vsftpd.conf 
14. 给文件每行加注释
sed -r -i 's/^/#/' filename
sed -r '/(^#|^$)/d;s/(.*)/#&/g' inittab
15. 每指定行加注释
sed -r -i '10,$s/^/#/' filename
sed -r '3,$s/^#*/#/' filename	

16. 取得网卡IP(除ipv6以外的所有IP)
[root@openvpn grep]#  ip a | grep -oE "([0-9]{1,3}\\.){3}[0-9]{1,3}" | grep -v "127"

17. 获得内存使用情况
[root@centos shell]# free -m | awk '/^Mem/{a=$4/$2*100;print a "%"}'
11.7583%
18. 获得磁盘使用情况
[root@centos shell]# df -h |awk '/\\/$/{print $4/$2*100 "%"}'
88.1356%
19. 打印出/etc/hosts文件的最后一个字段(按空格分隔)
[root@centos shell]# awk '{print $NF}' /etc/hosts
VM-4-10-centos
localhost
localhost4
VM-4-10-centos
localhost
localhost6
20. 打印指定目录下的目录名
[root@centos shell]# ll |grep '^d' |awk '{print $NF}'
a

以上是关于shell三剑客练习 05的主要内容,如果未能解决你的问题,请参考以下文章

SHELL编程四剑客练习--grep

SHELL编程四剑客练习--awk

用shell awk解决所有牛客网-SHELL篇练习问题

用shell awk解决所有牛客网-SHELL篇练习问题

shell脚本正则表达式三剑客之一(grep,egrep)

Linux文本三剑客之一——awk详解——awk看这两篇就够啦~PS:文末有练习,来练练手吧