Search code examples
bashawksedgrep

Bash get last sentence in line


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.


Solution

  • 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.