Wednesday, October 27, 2021

[SOLVED] sed: keep last chunk of each word containing chunk pattern

Issue

Willing to keep only last chunk of words containing a dot (still keeping words not containing pattern):

sed seems to only match last word:

$ echo "Here this.is.a.start is a very.nice.String  that.is.the.end yes " | sed 's/.*[ ]\([^ ]*\.\)\([^ ]*\)[ ].*/\2/g'
end

$ echo "Here this.is.a.start is a very.nice.String  that.is.the.end yes " | sed 's/.*[ ]\([^ ]*\.\)\([^ ]*\)[ ].*/\1/g'
that.is.the.

how should I do to get this result ? :

echo "Here this.is.a.start is a very.nice.String  that.is.the.end yes " | sed s\\\g
Here start is a String  end yes

Solution

You can use

sed -E 's/([^ .]+\.)+([^ .]+)/\2/g'
sed -E 's/([^[:space:].]+\.)+([^[:space:].]+)/\2/g'

The [:space:] will account for any whitespace, not just a space char.

See an online sed demo:

echo "     * {@link my.tailor.is.rich but( ther.is.a.hole.in.my.pants )}. " | \
 sed -E 's/([^ .]+\.)+([^ .]+)/\2/g'
# => "     * {@link rich but( pants )}. "

Details

  • ([^ .]+\.)+ - one or more occurrences of
    • [^ .]+ - any one or more chars other than a space and a dot
    • \. - a dot
  • ([^ .]+) - Group 2: any one or more chars other than a space and a dot.


Answered By - Wiktor Stribiżew