Let's say I have a string like this:

title="2010-09-11 11:22:45Z"

How can I grep the date itself and disregard the quotes/title/Z?

The file can contain more strings like:

title="2010-09-11 11:22:45Z"
title="disregard me"

So I only want to grep timestamps with a single grep command.

  • 1
    sed 's/.*="\(.*\)"/\1/' – groxxda Jul 26 '14 at 18:19
  • @Groxxda thanks, but is it possible in grep, too? – polym Jul 26 '14 at 18:20
  • grep -o '".*"' | tr -d '"' – groxxda Jul 26 '14 at 18:21
  • I don't get why this question is downvoted so often. It's a simple question: grep timestamps using one command only. – polym Jul 26 '14 at 19:10
  • Are hours and day always two digits? – groxxda Jul 26 '14 at 20:16

With GNU grep, you can do:

$ echo 'title="2010-09-11 11:22:45Z"' | grep -oP 'title="\K[^"]+'
2010-09-11 11:22:45Z
  • 2
    no explanation, what is K? – Buttle Butkus Oct 27 '16 at 0:41
  • 3
    @ButtleButkus: \K is PCRE syntax, which don't include anything in left side of \K in matching result, or in Perl way, don't include in $&. – cuonglm Oct 27 '16 at 4:25
  • thanx a lot man. If you could include also that each flag it's doing.. Would be perfect. – DimiDak Nov 27 at 16:12
grep -oP '[0-9-]{10} [0-9:]{8}' filename

This should work only on the GNU version of grep:

<file.html grep -oP "(?<=title\=\")\d+-\d+-\d+"

Example on regex101 here.


If input would be in this format only then below command will easily solve your problem

echo "title=\"2010-09-11 11:22:45Z\"| cut -d '"' -f2 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.