bash grep 日志文件中特定日期的所有行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10969531/
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
grep all lines for a specific date in log-files
提问by twan163
I need to grep all lines for "yesterday" in /var/log/messages. When I use following snippet, I get zero results due to the fact that the dates are in the format "Jun 9". (It doesn't show here, but in the log file, the days of the month are padded with an extra space when smaller than 10).
我需要在 /var/log/messages 中搜索“昨天”的所有行。当我使用以下代码段时,由于日期的格式为“Jun 9”,我得到的结果为零。(这里没有显示,但在日志文件中,当月的天数小于 10 时,会用额外的空格填充)。
cat /var/log/messages | grep `date --date="yesterday" +%b\ %e`
When I enter
当我进入
$ date --date="yesterday" +%b\ %e
on the commandline, it returns yesterday's date, with padding.
在命令行上,它返回昨天的日期,带填充。
But when I combine it with grep and the backticks, the extra padding gets suppressed, which gives me zero results.
但是当我将它与 grep 和反引号结合起来时,额外的填充被抑制了,这给了我零结果。
What do I need to change so that the "date" gets evaluated withextra padding?
我需要更改什么才能使用额外的填充来评估“日期” ?
回答by John Lawrence
You should be able to fix this by putting quotes around the backticks:
您应该能够通过在反引号周围加上引号来解决此问题:
cat /var/log/messages | grep "`date --date="yesterday" +%b\ %e`"

