sed 过滤日志:
sed -n '/开始日期/,/结束日期/p'
贴个日志做参考:
51.222.253.12 - - [08/Jan/2022:02:53:30 +0800] "GET /robots.txt HTTP/1.1" 301 169 "-" "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)" "-"
51.222.253.12 - - [08/Jan/2022:02:53:31 +0800] "GET /robots.txt HTTP/1.1" 404 153 "-" "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)" "-"
51.222.253.16 - - [08/Jan/2022:02:53:33 +0800] "GET / HTTP/1.1" 301 169 "-" "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)" "-"
54.36.148.148 - - [08/Jan/2022:02:57:02 +0800] "GET / HTTP/1.1" 302 35 "-" "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)" "-"
注意事项:
1.转义,当变量中日期需求转义,直接赋值无效
错误范例:STARTDAY=`echo $START|sed ‘s#/#\\/#g’`
START="08/Jan/2022"
END="08/Jan/2022"
#我是方法是导出到文件,再赋值
echo $START|sed 's#/#\\/#g' > /tmp/name
STARTDAY=`cat /tmp/name`
echo $END |sed 's#/#\\/#g' > /tmp/name
ENDDAY=`cat /tmp/name`
2.sed引用变量:
#第一种:单引号变双引号
sed -n "/$START/,/$END/p"
#第二种:将变量使用单引号包含双引号
sed -n '/'"$START"'/,/$END/p'
3.sed 过滤日期,如果精确到时、分、秒!所以过滤时分时请再三确认!!!
开始日期日志中没有,则否则过滤失败!!!
结束日期日志中没有,则直接过滤到日志最后!!!
发表评论