linux 四剑客 find grep awk sed 日志切分

一. find

格式
find /tmp -name “.log" -exec 命令名 - {} ;
find /tmp -name "
.log” | xargs rm -rf [有局限性 只支持r m]

参数;
-size k M G 文件大小
-type d f b s c 文件类型
-maxdepth 1 查找深度
-atmie n #n为数字,意义为n天之前的【一天之内】被access过的文件
-ctime n #n为数字,意义为n天之前的【一天之内】被change过状态的文件
-mtime n #n为数字,意义为n天之前的【一天之内】被modify过的文件
-perm 文件权限755

二 grep [egrep是grep的扩展] [ acivn]
格式:
grep -n --color “^root” /etc/passwd
参数:
–color 颜色
-c 只输出匹配行的计数
-n 对匹配的内容显示行号
-v 显示不包含匹配文本的所有行 [ 取反] grep -v “#” vsftpd
-i 查找的字符串不区分大小写
-l 查询多文件时只输出包含匹配字符的文件名
[root@localhost ggg]# grep -l “this” 123.*
123.gg
123.log
123.txt
-h 查询多文件时不显示文件名
[root@localhost ggg]# grep “this” 123.*
123.gg:this is a test
123.log:this is a test
123.txt:this is a test
[root@localhost ggg]# grep -h “this” 123.*
this is a test
this is a test
this is a test

-B before 之前的 -A after 之后的 -C 前后的(中间的)
grep “RETVAL=0” -B 5 vsftpd
grep “RETVAL=0” -A 5 vsftpd
grep “RETVAL=0” -C 5 vsftpd
grep 30 -B 10 bai.txt
grep 20 -A 10 bai.txt
grep 25 -C 10 bai.txt

三.awk
格式
awk -F '":" ‘{print $1}’ 文件名
[root@localhost ggg]# awk -F “g” ‘{print 1 , 1, NF}’ g.txt
this test
[root@localhost ggg]# awk -F “g” ‘{print 1 " " 1"--" NF}’ g.txt
this–test
注意:
打印多列 用逗号 ,
在打印多列之间用添加自定义符号拼接 ,用双引号 " " 添加拼接

参数:
NF: 表示最后一列
NF-1: 倒数第二列
NR: 表示行号 awk ‘NR==2{print $0}’ 123.log
案例:
[root@localhost ggg]# ifconfig eth1 | grep “Bca” | awk ‘{print $2}’| awk -F: ‘{print $2}’|awk -F “.” ‘{print $1"-"$2"-"$3"-"$4}’
192-168-2-100

四. sed
格式 :
‘s#ni hao#welcome#g’ 123.txt [ 替换]

参数:
i 在指定行的前面添加
a 在指定行的后面添加

猜你喜欢

转载自blog.csdn.net/weixin_44562661/article/details/86533984