Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed to remove everything after a [[:space:]]

Tags:

grep

sed

How can I remove everything after a whitespace? I can do it if I specify the colon ':' rather than [[:space:]].

$ cat t.sh
echo "DW_Prod\\Facets\\UNRCH_MBRS: UNRCH_Members.sql" | \
    sed -r -e 's#.*\(DW_Prod.*\)[[:space:]].*#\\1#'

$ ./t.sh
DW_Prod\Facets\UNRCH_MBRS: UNRCH_Members.sql
like image 562
lit Avatar asked Mar 03 '26 20:03

lit


1 Answers

You are enabling ERE metachars with -r and then disabling the ERE capture groups (...) by escaping the delimiters \(...\) and disabling the backreference \1 by escaping the backslash \\1. Try this:

$ echo "DW_Prod\\Facets\\UNRCH_MBRS: UNRCH_Members.sql" | \
    sed -r -e 's#.*(DW_Prod.*)[[:space:]].*#\1#'
DW_Prod\Facets\UNRCH_MBRS:

All you really need though is:

$ echo "DW_Prod\\Facets\\UNRCH_MBRS: UNRCH_Members.sql" |
    sed 's#[[:space:]].*##'
DW_Prod\Facets\UNRCH_MBRS:
like image 194
Ed Morton Avatar answered Mar 05 '26 09:03

Ed Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!