3

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:' word.

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

рüффп
  • 1,707
Sachin
  • 39
  • 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 '18 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 '18 at 15:39
  • you want only the first line or the text after until on each lines? Because you said: I should get only Sun Nov 24 12:11:28 EST 2019. It's not clear if it's only the 1st line example or the result of the whole. – рüффп May 16 '19 at 22:12

5 Answers5

8

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
RudiC
  • 8,969
6

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.

glenn jackman
  • 85,964
  • Thanks, did not know about \K. Until now I always used lookbehinds, but this saves three characters ;-) – pLumo Aug 20 '18 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 '18 at 15:04
3

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}'
igiannak
  • 750
2

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

Alexander
  • 1,416
2

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

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