一、grep
grep [Globally search a Regular Expression and Print]
grep -E = egrep
grep 格式
grep 匹配条件 处理文件
grep root passwd | 过滤root关键字 |
grep -i root passwd | 忽略大小写 |
grep -E "\<root" passwd | root字符之前不能有字符 |
grep -E "root\>" passwd | root字符之后不能有字符 |
grep -数字 | 显示过滤行以及上面几行和下面几行 |
grep -n | 显示匹配的行所在行号 |
grep -A | 显示过滤行以及下面几行 |
grep -B | 显示过滤行以及上面几行 |
grep -v | #反向过滤 |
[root@westoslinux mnt]# grep -e bash -e sh passwd ##多条件匹配
[root@westoslinux mnt]# egrep "bash|sh" passwd
[root@westoslinux mnt]# grep -E "bash|sh" passwd
[root@westoslinux mnt]# grep root passwd
[root@westoslinux mnt]# grep -i root passwd ##不区分大小写
[root@westoslinux mnt]# grep -E "\<root" passwd##root字符之前不能有字符
[root@westoslinux mnt]# grep -E "root\>" passwd##root字符之后不能有字符
[root@westoslinux mnt]# grep -E "\<root\>" passwd##包含root的
[root@westoslinux mnt]# grep -n chroot passwd ##显示匹配的行所在行号
[root@westoslinux mnt]# grep -2 chroot passwd#显示过滤行以及上面2行和下面2行
[root@westoslinux mnt]# grep -A1 chroot passwd##显示过滤行以及下面1行
[root@westoslinux mnt]# grep -B1 chroot passwd##显示过滤行以及上面1行
[root@westoslinux mnt]# grep root passwd -v ##反向过滤
grep字符数量匹配规则
^westos | 以westos开头 |
westos$ | 以westos结尾 |
w....s | w开头s结尾中间4个任意字符 |
.....s | s结尾前面5个任意字符 |
* | 字符出现任意 |
? | 0到1次 |
+ | 1次到任意次 |
{n} | n次 |
{m,n} | m到n次 |
{0,n} | 0-n次 |
{,n} | 0-n次 |
{m,} | 最少m次 |
(lee){2} | lee字符串出现2次 |
练习脚本:
请显示系统中能被su命令切换的用户名称
[root@westos137 mnt]# grep -E 'bash$|sh$' /etc/passwd | cut -d : -f 1
root
westos
二、sed
命令格式
sed 参数 命令 处理对象
sed 参数 处理对象 -f 处理规则文件
对字符的处理
p ##显示
sed -n 5p westos ##显示第五行
sed -n 3,5p westos ##显示3到5行
sed -ne "3p;5p westos ##显示3和5行
sed -ne 1,5p westos ##1-5行
sed -ne '5,$p' westos ##5到最后一行
sed -n '/^#/p' fstab ##显示以#开头的行
d ##删除
sed 5d fstab ##删除第五行
sed '/^#/d' fstab ##把#开头的行删除
sed '/^UUID/!d' fstab ##除了UUID以外的行都删除
sed -e '5,$d' westos
a ##添加
sed -e '$a hello world' fstab
sed -e '$a hello\nworld' fstab
sed -e '/^#/a hello world' fstab
c ##替换
sed -e '/^#/c hello world' fstab
sed '5chello world' westos
删除#号开头的和空行
将结果导入file
i ##插入
sed '2ihello westos' westos ##第2行之前插入hello westos
r ##插入
sed '5r haha' westos
字符替换
sed 's/:/###/g' westos
sed 's/:/###/' westos
sed's/:/###/g' westos
sed'1,5s/:/###/g' westos
sed'1s/:/###/g' westos
sed'1s/:/###/g;5s/:/###/g' westos
sed'/lp/,/shutdown/s/:/###/g' westos
sed's/\//####/g' westos
sed's@/@####@g' westos
sed 's@/@####@g' -i westos ##把sed处理的内容保存到westos文件中
练习及脚本
Apache_port.sh
此脚本接入数字
http的端口就改为此数字
假设selinux为关闭状态
例如:
sh Apache_port.sh
ERROR: Pleaase input port number following script !!
sh Apache_port.sh 8080
apache的端口会被修改为8080
vim Apache_port.sh
#!/bin/bash
[ -z "$*" ] &&{ ##脚本后为空时显示
echo ERROR:Pleaase input port number following script !!
exit
}
rpm -q httpd &> /dev/null || { ##判断是否下载httpd
echo "$* is not installd!!"
exit
}
netstat -antlupe | grep -E "\<$*\>" &> /dev/null &&{ ##判断脚本后接入数字的端口是否正在使用
echo "$* is in used!!"
exit
}
sed "/^Listen/cListen $*" -i /etc/httpd/conf/httpd.conf ##把sed处理的内容保存到配置文件中
systemctl restart httpd
三、awk
awk -F 分隔符 BEGIN{ }{ }END{ } FILENAME
NR #行数
NF #列数
FILENAME #文件名称本身
westos #westos变量值
“westos” #westos字符串/bash$/ #条件
/条件1|条件2/ #条件1或者条件2
/条件1/||/条件2/ #条件1或者条件2
/条件1/&&/条件2/ #条件1并且条件2$0 #所有的列
$1 #第一列
$2 #第二列
$3 #第三列
#/etc/passwd文件的第六列没有home关键字并且以bash结尾的行
awk -F : '$6!~/home/&&/bash$/{print}' /etc/passwd
[root@westoslinux mnt]# awk -F : 'BEGIN{print start}' passwd ##不加“”表示变量
[root@westoslinux mnt]# awk -F : 'BEGIN{START=0}{print START}END{print "START"}' passwd ##加“”表示字符
[root@westoslinux mnt]# awk -F : 'BEGIN{print "start"}{print NF}END{print "end"}' passwd ##NF为列数
[root@westoslinux mnt]# awk -F : 'BEGIN{N=0}{N++}END{print N }' passwd
17 ##输出行数
[root@westoslinux mnt]# wc -l passwd
17 passwd
[root@westoslinux mnt]# awk -F : '/sbin/{print $0}' passwd ##显示全文包含sbin的行
[root@westoslinux mnt]# awk -F : '!/sbin/{print $0}' passwd ##不包含sbin 的行
root:x:0:0:root:###root:###bin###bash
[root@westoslinux mnt]# awk -F : '!/sbin|test/{print $0}' passwd ##不包含sbin或不包含test的行
root:x:0:0:root:###root:###bin###bash
[root@westoslinux mnt]# awk -F : '!/sbin/&&!/test/{print $0}' passwd
root:x:0:0:root:###root:###bin###bash ##不是sbin并且不是test
[root@westoslinux mnt]# awk -F : '/sbin/||/test/{print $0}' passwd
[root@westoslinux mnt]# awk -F : '/sbin/&&/test/{print $0}' passwd
test:test:sbin ##并且
[root@westoslinux mnt]# awk -F : '/sbin|test/{print $0}' passwd
[root@westoslinux mnt]# awk -F : '/sbin/&&/test/{print $0}' passwd
test:test:sbin
[root@westoslinux mnt]# awk -F : '/sbin/&&/test/{print $1}' passwd
test
[root@westoslinux mnt]# awk -F : '/sbin/&&/test/{print $1$2}' passwd
testtest
[root@westoslinux mnt]# awk -F : '/sbin/&&/test/{print $1 $2}' passwd
testtest
[root@westoslinux mnt]# awk -F : '/sbin/&&/test/{print $1,$2}' passwd
test test
[root@westoslinux mnt]# awk -F : '$7~/nologin/{print $0}' passwd ##第7列是nologin的行
[root@westoslinux mnt]# awk -F : '$7!~/nologin/{print $0}' passwd ##第7列不是nologin
课后练习:
统计在系统中能su切换的并且用户加目录不在/home下的用户数量
[root@westos137 mnt]# vim westos.sh
[root@westos137 mnt]# cat westos.sh
awk -F : '$6!~/home/&&/bash$|sh$/{print $0}' passwd | wc -l
[root@westos137 mnt]# sh westos.sh
1
要求输入ens3会出现ens3的Ip ,ens9会出现ens9的ip
[root@westos137 mnt]# vim show_address.sh
#!/bin/bash
[ -z "$*" ] &&{ ##脚本之后是否为空
echo Please input
exit
}
DEV_MESSAGE=$(ifconfig "$*" 2> /dev/null )||{ ##脚本之后的网卡不存在时
echo $* is not exist!!
}&&{
echo $DEV_MESSAGE | awk '/inet\>/{print $6}' ##脚本之后的网卡存在时指定输出
}
要求抓取所有网卡名称
[root@westos137 mnt]# vim sh_netdev.sh
#!/bin/bash
awk -F : '/:/&&!/lo/{print $1}' /proc/net/dev