文本处理三剑客
grep:查找
sed: 替换
awk : 计算
grep命令使用来查找内容的。都支持管道,输入重定向。
grep 用法:
grep centos file.txt file1.txt file2.txt /etc/*conf
统计:-c
显示行号: -n
显示文件名称:-l
隐藏文件名称:-h
反向查找:-v
忽略大小写:-i
递归: -r
静默: -q
查找哪些log日志中包含了error。`
[root@localhost log]# grep -rli error /var/log/*
anaconda/syslog
anaconda/program.log
anaconda/packaging.log
anaconda/journal.log
messages
secure
[root@localhost log]# find /etc/ -type f -name “*conf” | xargs grep -l zabbix
/etc/zabbix/zabbix_agent2.d/plugins.d/mongodb.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/postgresql.conf
/etc/zabbix/zabbix_agent2.conf
[root@localhost log]#
sed
sed = streaming editor
sed命令一般都不会影响,他会处理好结果输出屏幕。
p: 打印
需要 -n 配合使用
sed -n ‘1,10p’ file.txt
sed -n ‘/^#/p’ file.txt
打印出10行
d: 删除
sed ‘50,$d’ file.txt
删除从50行到最后一行。
a:append,在模式匹配到行的后面, 添加文字
sed ‘1,2acreate table ‘ file.txt
i: insert ,,在模式匹配到行的前面, 添加文字
s: 替换
sed -i.bak ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/config
最后编辑:严锋 更新时间:2025-05-09 15:48