This question already has an answer here:

Here is the data:

Valid from: Fri Nov 24 12:11:28 EST 2017 until: Sun Nov 24 12:11:28 EST 2019
Valid from: Fri Feb 24 13:21:19 EST 2017 until: Thu Feb 24 13:31:19 EST 2022
Valid from: Tue Dec 20 12:01:19 EST 2016 until: Fri Dec 20 12:11:17 EST 2041

I want to extract only data which is written after until.

For example I should get only Sun Nov 24 12:11:28 EST 2019.

Thanks in advance.

Thanks

marked as duplicate by GAD3R, Sundeep, Isaac, n.st, Sparhawk Aug 21 at 2:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Does the solution require grep, or are you just suggesting that grep might be a solution to your real question, which is "[how do I] return specific data from [a] string?" – Christopher Schultz Aug 20 at 13:55
  • The title references "Grep" but there's no reason why the grep command should be involved in this at all, the way it's presented. @GAD3R is correct; this is a dupe. It has nothing to do with returning specific lines and everything to do with throwing away everything before a specific pattern of characters, which the other question already answers perfectly. – Monty Harder Aug 20 at 15:39

Try also

awk '{sub (/^.*until: /, "")} 1' file
Sun Nov 24 12:11:28 EST 2019
Thu Feb 24 13:31:19 EST 2022
Fri Dec 20 12:11:17 EST 2041

or even shorter

sed 's/^.*until: //' file
Sun Nov 24 12:11:28 EST 2019
Thu Feb 24 13:31:19 EST 2022
Fri Dec 20 12:11:17 EST 2041
  • 1
    can also use "until: " as the field separator and print the 2nd field: awk -F 'until: ' '{print $2}' – glenn jackman Aug 20 at 14:48

With GNU grep

echo "$string" | grep -oP 'until: \K.*'

That matches the word "until: ", then the \K forgets about everything that was just matched, then the .* can match everything after that.

  • Thanks, did not know about \K. Until now I always used lookbehinds, but this saves three characters ;-) – RoVo Aug 20 at 14:55
  • 2
    one of the drawbacks of lookbehinds is that the patterns have to have a fixed width. That restriction does not apply with \K – glenn jackman Aug 20 at 15:04

By setting the awk delimiter to "until:" you can get the subsequent string while this text exist on your string. awk -F'until:' '{print $2}'

echo "Valid from: Fri Nov 24 12:11:28 EST 2017 until: Sun Nov 24 12:11:28 EST 2019" | awk -F'until:' '{print $2}'

grep -o 'until: [^V]*' file | cut -d ':' -f 2-

  • why [^V] and not just . ? – RoVo Aug 20 at 14:57
  • first sample wasn't split for lines, So I was grepping until next 'V' in 'Valid' word. I Agree, That now it loooks weered . – Alexander Aug 20 at 15:01

If format will always be the same you can also try :

cut -d':' -f 5- /path-to-file 

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