This question already has an answer here:

I'm trying to use sed to remove any subdomain prefix from a list of hostnames, but it's not replacing anything when I use a regular expression. Here's the simplified example that's not working:

echo "subdomainxyz.example.com" | sed 's/[a-z]+\.example\.com//'

When I hard-code a particular subdomain in there, it works just fine:

echo "subdomainxyz.example.com" | sed 's/subdomainxyz\.example.com/example.com/'

marked as duplicate by G-Man, Jeff Schaller, Stephen Harris, Kusalananda, Braiam Jul 30 '16 at 17:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @Gilles My feelings wouldn't be hurt if you decide to close this as already answered; however, because the question is posed differently, I'm a fan of keeping it open because people may be more apt to find the solution if they are searching something similar. IMO anything within reason that increases the chances that a SE result comes up in the search engines is a benefit to the community. – sa289 Oct 12 '15 at 4:12
  • 1
    That's exactly why we don't delete duplicates: they will keep coming up in search engines. – Gilles Oct 12 '15 at 9:30
  • But this question doesn’t make a good signpost.  It’s about finding, matching, and searching — and it doesn’t mention any of those key words.  I wonder whether somebody else who has this problem is really likely to Google for “replacement” or “replacing”.  P.S. It might be useful to edit the canonical question to include a gratuitous mention of the word “search” simply to make it easier to locate. – G-Man Jul 29 '16 at 23:10

I figured out the answer before I submitted the question but since I had typed it up already, I thought I'd post it in case it's helpful for anyone.

The key thing that was needed was to add the -r flag (may be -E on some versions) to tell sed to support extended regular expressions which is needed to use the character class / range syntax and the plus operator [a-z]+. This is just like you have to do with grep (for some reason I thought sed defaulted to extended regular expressions but that was incorrect).

Here's a working example:

echo "subdomainxyz.example.com" | sed -r 's/[a-z]+\.example\.com/example.com/'

EDIT (credit steeldriver):

Simply escaping the plus sign works without -r being required. Example:

echo "subdomainxyz.example.com" | sed 's/[a-z]\+\.example\.com/example.com/'
  • 2
    AFAIK the only part of your expression that is not supported by the basic regular expression (BRE) syntax is the + operator: you could have used \+ or (POSIXly) \{1,\} to mean one-or-more of the preceding RE without the -r or -E switch. – steeldriver Oct 10 '15 at 23:31
  • @steeldriver You're right \+ did the trick without -r being required. I'll edit my answer. – sa289 Oct 11 '15 at 23:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.