Extract date from string in shell script

2019-08-27 18:58发布

I want to get a date which is in a string with shell script for an example:

    dddddddddddddddansdpaojhndpaosdjnad
    dddddddddddddddansdpaojhndpaosdjnad
    dddddddddddddddansdpaojhndpaosdjnad
    dddddddddddddddansdpaSun Apr 07 10:38:34 EEST 2013jnad
    dddddddddddddddansdpaojhndpaosdjnad

How I can extract a date wich is in a string with shell script?

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-27 19:52

I don't know if your example text is really reflecting your real data. And I think the EEST is a typo? If work on the example you gave:

dddddddddddddddansdpaojhndpaosdjnad
dddddddddddddddansdpaojhndpaosdjnad
dddddddddddddddansdpaojhndpaosdjnad
dddddddddddddddansdpaSun Apr 07 10:38:34 EEST 2013jnad
dddddddddddddddansdpaojhndpaosdjnad

this line should do the job:

grep -o '[A-Z].*[0-9]'

for example:

kent$  echo "    dddddddddddddddansdpaojhndpaosdjnad
    dddddddddddddddansdpaojhndpaosdjnad
    dddddddddddddddansdpaojhndpaosdjnad
    dddddddddddddddansdpaSun Apr 07 10:38:34 EEST 2013jnad
    dddddddddddddddansdpaojhndpaosdjnad"|grep -o '[A-Z].*[0-9]'
Sun Apr 07 10:38:34 EEST 2013
查看更多
登录 后发表回答