文本处理工具
一、grep [Globally search a Regular Expression and Print]
grep
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 ##反向过滤
cat /etc/passwd -b | head -n 18 > westos
ls
cat westos
grep root westos #过滤root关键字,模糊搜索也叫贪婪模式
grep root westos -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次 |
vim lee
cat lee
grep w.s lee #w开头s结尾中间1个任意字符
grep w..s lee #w开头s结尾中间2个任意字符
grep w...s lee #w开头s结尾中间3个任意字符
grep w.*s lee #w开头s结尾中间任意字符
grep -E 'w.{4}s' lee
grep -E 'w.{5}s' lee
grep -E 'w.{3}s' lee
grep -E 'w.{1,}s' lee
grep -E 'w.{1,3}s' lee
grep -E 'w.?s' lee
grep -E 'w.+s' lee
grep -E 'w.{,3}s' lee
grep -E 'we{,3}s' lee
grep -E 'web{,3}s' lee #过滤出以w开始中间有b出现0到3次,以s结尾的
grep -E 'w(eb){,3}s' lee #过滤出以w开始中间有eb出现0到3次,以s结尾的
练习脚本:
请显示系统中能被su命令切换的用户名称
grep -E "bash$|sh$" /etc/passwd | cut -d : -f 1
grep sbin /etc/passwd -v | cut -d : -f 1
二、sed
命令格式:
sed 参数 命令 处理对象
sed 参数 处理对象 -f 处理规则文件
对字符的处理
cat /etc/passwd | head -n 10 > westos
vim westos
cat westos
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' westos ##显示以#开头的行
对第十行进行了修改,添加了#号,以便完成实验,也可一开始设置好
d ##删除
sed 5d westos ##删除第五行
sed '/^#/d' westos ##把#开头的行删除
sed '/^UUID/!d' westos ##除了UUID以外的行都删除
sed -e '5,$d' westos ##五行以后都删掉
a ##添加
sed -e '$a hello world' westos ##末尾添加hello world
sed -e '$a hello\nworld' westos ##末尾添加hello后换行添加world
sed -e '/^#/a hello world' westos##所有#开头的行后添加一行hello world
c ##替换
sed -e '/^#/c hello world' westos## #开头的行换为hello world
sed '5chello world' westos ##第五行替换为hello world
w ##把符合的行写到指定文件中
sed '/^UUID/w westofile' westos ##把westos中UUID开头的行写入westosfile中
i ##插入
sed '5ihello westos' westos
r ##整合文件
sed '5r haha' westos
sed '5r haha' -i westos #把原文件修改
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文件中
练习及脚本
Apache_port.sh
此脚本接入数字
http的端口就改为此数字
假设selinux为关闭状态
例如:
sh Apache_port.sh
ERROR: Pleaase input port number following script !!
sh Apache_port.sh 8080
apache的端口会被修改为8080
#!/bin/bash
setenforce 0 #将selinux转换为警告模式
[ -z $1 ] && {
echo -e "请写上端口号"
exit
}
dnf install httpd -y &> /dev/null && {
echo "httpd服务以安装"
} || {
echo -e "请安装httpd服务"
}
sed " /^Listen/c Listen $1 " -i /etc/httpd/conf/httpd.conf
firewall-cmd --permanent --add-port=$1/tcp
echo "火墙设置允许端口‘$1’通过"
firewall-cmd --reload
echo "火墙以重启"
systemctl restart httpd
echo "端口号以修改为‘$1’"
三、awk
awk -F 分隔符 BEGIN{}{}END{} FILENAME
NR #行数
NF #列数
FILENAME #文件名称本身
westos #westos变量值
“westos” #westos字符串
cat westos
awk -F : 'BEDTN{print westos}{print $1}END{print end}' westos
awk -F : 'BEDTN{print "westos"}{print $1}END{print "end"}' westos
awk -F : 'BEDTN{N=0}{N++}END{print N}' westos #统计数量
/bash$/ #条件
/条件1|条件2/ #条件1或者条件2
/条件1/||/条件2/ #条件1或者条件2
/条件1/&&/条件2/ #条件1并且条件2
参数 | 说明 |
---|---|
$0 | #所有的列 |
$1 | #第一列 |
$2 | #第二列 |
$3 | #第三列 |
cat westos
awk -F : '{print $1}' westos #显示第一行
awk -F : '/bash$/{print $1}' westos #
awk -F : '/bash$/&&/root/{print $1}' westos
awk -F : '/bash$/&&!/root/{print $1}' westos
awk -F : '/bash$/||/root/{print $1}' westos
awk -F : '/bash$/||/halt/{print $1}' westos
awk -F : '/bash$|halt/{print $1}' westos
awk -F : '!/bash$|halt/{print $1}' westos
awk -F : '$7~/bash/{print $1}' westos #第七列没有bash,显示第一列
awk -F : '$7!~/bash/{print $1}' westos
awk -F : '$7!~/bash/{print $0}' westos
#/etc/passwd文件的第六列没有home关键字并且以bash结尾的行
awk -F : '$6!~/home/&&/bash$/{print}' /etc/passwd
课后练习:
统计在系统中能su切换的并且用户加目录不在/home下的用户数量
vim su.sh
awk -F : 'BEGIN{N=0}$6!~/^\/home/&&/bash$|sh$/{print $0;N++}END{print N}' /etc/passwd
笔试题:
ps ax -o %men #查询内存,统计其中数字的总和?
[root@westosa mnt]# vim jia.sh
[root@westosa mnt]# cat jia.sh
ps ax -o %mem | awk 'BEGTN{N=0}!/%MEM|0.0/{N+=$1}END{print N}'
[root@westosa mnt]# sh jia.sh
89.6
[root@westosa mnt]#