bash 使用 awk 命令递归搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27769973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursive search using awk command
提问by atul329
I have 100 log files and I want awk to search for a given pattern between a given timestamp recursively. Log files look something like this
我有 100 个日志文件,我希望 awk 递归搜索给定时间戳之间的给定模式。日志文件看起来像这样
2010-03-24 07:00:01 ZZZZC941948879 RUFFLES 222.222.222.222 GET / - 80 - 220.181.7.113 HTTP/1.1
2010-03-24 07:00:23 ZZZZC941948879 RUFFLES 222.222.222.222 GET
Code is
代码是
awk -v "b=$date1" -v "e=$date2" ' >= b && <= e' log.txt > output
grep -i "21things" output
I am able to search for the pattern but for single file only. Is it possible using awk command to search recursively?
我可以搜索模式,但只能搜索单个文件。是否可以使用 awk 命令进行递归搜索?
Thanks for the help..!!
谢谢您的帮助..!!
回答by glenn Hymanman
If your logs are all in the same directory, use a shell wildcard:
如果您的日志都在同一目录中,请使用 shell 通配符:
awk -v "b=$date1" -v "e=$date2" ' >= b && <= e' *.log
Note that awk can do what grep does, so you don't need the temp file:
请注意,awk 可以执行 grep 所做的操作,因此您不需要临时文件:
awk -v "b=$date1" -v "e=$date2" -v patt="21things" '
>= b && <= e && tolower(##代码##) ~ patt
' *.log
If you have GNU awk, use -v IGNORECASE=1
and remove the tolower
function.
如果您有 GNU awk,请使用-v IGNORECASE=1
并删除该tolower
函数。