1.grep
gerp :[Globally search a Regular Expression and Print]
grep -E = egrep
grep格式
**基本格式:grep 匹配条件 处理文件**
grep root passwd(file,/etc/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 :反向过滤
**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次
2.sed
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 '5,$p' westos :显示第5行到最后行
sed -n '/^#/p' fstab :显示以#号开头的行
**d** 删除
sed 5d westos :删除第五行
sed '/^#/d' fstab(etc/fstab:file):把#开头的行删除
sed '/^UUID/!d' fstab :除了UUID以外的行都删除
**a** 添加
sed -e '$a hello world' fstab
sed -e '$a hello\world' fstab
sed -e '/^#/a hello world' fstab
**c** 替换
sed -e '/^#/c hello world' fstab
sed '5chello world' westos
**w** 把符合的行写到指定文件中
sed '/^UUID/w westofile' westos :把westos中UUID开头的行写入westosfile中
**i** 插入
sed '5ihello westos' westos
**r** 整合文件
sed '5r haha' westos
**sed 字符替换**
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文件中
3.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 :第三列
实验
实验1
请显示系统中能被su命令切换的用户名称
实验2
Apache_port.sh:此脚本接入数字,http的端口就改为此数字
假设selinux为关闭状态
例如:sh Apache_port.sh
ERROR: Pleaase input port number following script !!
sh Apache_port.sh 8080
apache的端口会被修改为8080
实验3
统计在系统中能su切换的并且用户加目录不在/home下的用户数量