bash Grep H:MM:SS 格式的时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23419141/
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 a time stamp in the H:MM:SS format
提问by Zerg12
Working on the file an need to grep the line with a time stamp in the H:MM:SS
format. I tried the following egrep '[0-9]\:[0-9]\:[0-9]'
. Didn't work for me. What am i doing wrong in regex?
处理文件需要使用H:MM:SS
格式中的时间戳对行进行 grep 。我尝试了以下 egrep '[0-9]\:[0-9]\:[0-9]'
。没有对我来说有效。我在正则表达式中做错了什么?
回答by TimK
$ date -u | egrep '\d{1,2}:\d{1,2}:\d{1,2}'
Fri May 2 00:59:47 UTC 2014
Try a site like http://regexpal.com/
尝试像http://regexpal.com/这样的网站
回答by BMW
Here is the fix:
这是修复:
grep '[0-9]:[0-9][0-9]:[0-9][0-9]'
If you need get timestamp only, and your grep is gnu grep.
如果您只需要获取时间戳,并且您的 grep 是 gnu grep。
grep -o '[0-9]:[0-9][0-9]:[0-9][0-9]'
and if you work more harder, limit on time format only:
如果你更努力地工作,只限制时间格式:
grep '[0-2][0-9]:[0-5][0-9]:[0-5][0-9]'
回答by GreNIX
Simplest way that I know of:
我所知道的最简单的方法:
grep -E '([0-9]{2}:){2}[0-9]{2}' file
If you need month and day also:
如果您还需要月份和日期:
grep -E '.{3,4} .{,2} ([0-9]{2}:){2}[0-9]{2}' file