一.grep
1.grep的基本命令参数
grep 匹配条件 处理文件
grep root passwd
grep -i root passwd
grep -E "\<root" passwd
grep -E "root\>" passwd
grep -数字
grep -n
grep -A
grep -B
grep -v
2.实验
242 grep root /etc/passwd //含有root的行
244 grep -E "root|nologin" passwd //含有root和nologin的行
245 grep -e root -e bash passwd //含有root和bash的行
246 grep -i root passwd //忽略大小写查找含root的行
247 grep -E "\<root\>" passwd //root前后都不能有字符的行
248 grep -n halt passwd //显示halt所在的行号
249 grep -5 halt passwd //显示含有关键字的前后各5行
250 grep -B5 halt passwd //含有关键字的前5行
251 grep -A5 halt passwd //含有关键字的后5行
252 grep root passwd -v //反选,不含有关键字的行
3.grep字符数量匹配规则
^westos
westos$
w....s
.....s
*
?
+
{n}
{m,n}
{0,n}
{,n}
{m,}
(lee){2}
4.实验
265 touch westosfile2
266 vim westosfile2
ws
was
wbs
waas
waaas
waaaas
waaaaas
wabababs
267 cat westosfile2
268 grep ws westosfile2
269 grep w.s westosfile2 //ws之间一个字符
270 grep w..s westosfile2 //ws之间两个字符
271 grep w...s westosfile2 //ws之间三个字符
272 grep -E "w.{2}s" westosfile2 //ws之间两个字符
273 grep -E "w.*s" westosfile2 //ws之间任意个字符
274 grep -E "wa*s" westosfile2 //ws之间任意个a
275 grep -E "wa{2}s" westosfile2 //ws之间两个a
276 grep -E "wa{1}s" westosfile2 //ws之间1个a
277 grep -E "wa{,1}s" westosfile2 //ws之间0-1个a
280 grep -E "wa?s" westosfile2 //ws之间任意个a
281 grep -E "wa+s" westosfile2 //ws之间1-任意个a
282 grep -E "wa{1,}s" westosfile2 //ws之间1-任意个a
283 grep -E "wa{1,2}s" westosfile2 //ws之间1-2个a
284 grep -E "wa{,2}s" westosfile2 //ws之间0-2个a
285 grep -E "wa{2,}s" westosfile2 //ws之间2-任意个a
286 grep -E "w(ab){2,}s" westosfile2 //ws之间2-任意个ab
5.练习脚本
请显示系统中能被su命令切换的用户名称
vim check_su.sh
grep -E "bash$|sh$" /etc/passwd | cut -d: -f 1
sh check_su.sh
二.sed
1.命令格式
sed 参数 命令 处理对象 sed 参数 处理对象 -f 处理规则文件
2.对字符的处理
(1)p命令
p
sed -n 5p westos
sed -n 3,5p westos
sed -ne "3p;5p westos
sed -ne 1,5p westos
sed -ne '5,$p' westos
sed -n '/^#/p' fstab
(2)d
d
sed 5d westos
sed '/^#/d' fstab
sed '/^UUID/!d' fstab
sed -e '5,$d' westos
(3)a
sed -e '$a hello world' fstab
sed -e '$a hello\nworld' fstab
sed -e '/^#/a hello world' fstab
(4)c
c
sed -e '/^#/c hello world' fstab
sed '5chello world' westos
(5)w
w
sed '/^UUID/w westofile' westos
sed '/nologin$/w westosfile' passwd //将passwd中的以nologin结尾的写入westosfile中
(6)i
i
sed '5ihello westos' westos
sed '/westos/i westoslinux' westos 在westos中的westos前面插入westoslinux
sed '/westos/a westoslinux' westos 在westos中的westos后面插入westoslinux
(7)r
r
sed '5r haha' westos
(8)sed 字符替换
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文件中
sed 's/a/b/g' test //g表示全局替换
sed 's/a/b/' test //表示替换每一行的第一列
sed 's/sbin/westos/g' passwd //全局的sbin替换成westos
sed '4,5s/sbin/westos/g' passwd //4,5行的sbin替换成westos
sed '$s/sbin/westos/g' passwd //最后一行的sbin替换成westos
sed '/lp/,/halt/s/sbin/westos/g' passwd//lp和halt之间的sbin替换成westos
sed 's/\//#####/g' passwd //全局的/替换成
sed 's@/@#####@g' passwd //全局的/替换成
sed 's@/@#####@g' -i passwd //替换的内容保存在原文件中
(9)练习及脚本
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
setenforce 0 &> /dev/null
[ -z "$1" ] && {
echo "Error:PLease input port number!!!"
exit
}
rpm -q httpd &> /dev/null || {
echo "Error: Apache is not running!"
exit
}
systemctl status httpd | grep "running" &> /dev/null || {
echo "Error : Apache is not running!!"
exit
}
netstat -antlupe | grep -E ":$1\>" &> /dev/null &&{
echo "Error:$1 is in used!"
exit
}
sed "/^Listen/c Listen $1" -i /etc/httpd/conf/httpd.conf
systemctl restart httpd
netstat -antlupe | grep http
|