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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:22:00  来源:igfitidea点击:

Grep a time stamp in the H:MM:SS format

bashgrep

提问by Zerg12

Working on the file an need to grep the line with a time stamp in the H:MM:SSformat. 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