0

assume, we got the following variable containing a string:

text="All of this is one line. But it consists of multiple sentences. Those are separated by dots. I'd like to get this sentence."

I now need the last sentence "I'd like to get this sentence.". I tried using sed:

echo "$text" | sed 's/.*\.*\.//'

I thought it would delete everything up to the pattern .*.. It doesn't.

What's the issue here? I'm sure this can be resolved rather fast, unfortunatly I did not find any solution for this.

chris137
  • 189
  • 1
  • 11
  • What would the expected output be if the last sentence was `The value of pi is 3.14.`? What if the sentence ended in some other punctuation mark like `!`? – Ed Morton May 20 '16 at 19:18
  • In this case the accepted answer indeed would not work. One possibility could be to include the common space after the dot as separator, so `. `. However this does not apply to my case so I'm going with anubhava's answer. – chris137 May 22 '16 at 06:41

2 Answers2

3

Using awk you can do:

awk -F '\\. *' '{print $(NF-1) "."}' <<< "$text"

I'd like to get this sentence.

Using sed:

sed -E 's/.*\.([^.]+\.)$/\1/' <<< "$text"

 I'd like to get this sentence.
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Totally forgot one could change the delimiter of awk. I'm always fixated on the spaces as default. Thanks! Awk is definitely nicer and I actually understand what happens! – chris137 May 20 '16 at 16:45
  • 1
    well, for exactly matching the output, should be `awk -F'[.] *' '{print $(NF-1)"." }'` Trim initial space and add a period. – karakfa May 20 '16 at 20:22
2

Don't forget the built-in

echo "${text##*. }"

This requires a space after the full stop, but the pattern is easy to adapt if you don't want that.

As for your failed attempt, the regex looks okay, but weird. The pattern \.*\. looks for zero or more literal periods followed by a literal period, i.e. effectively one or more period characters.

tripleee
  • 175,061
  • 34
  • 275
  • 318