4

Today I've come across this command for extracting domain name from domain controller's FQDN:

DOMAIN_NAME=$(echo $DC_PC | sed -r 's!^[^.]+\.!!')

What is the meaning of these exclamation marks in sed? How does it work?

  • That is called as Delimiter in sed – Rahul Sep 02 '16 at 09:58
  • 2
    Or just domain_name=${DC_PC#*.} (strictly speaking the equivalent would be ${DC_PC#[!.]*.} because of that + which is I don't think was intended) which would be a lot more portable, efficient and reliable. – Stéphane Chazelas Sep 02 '16 at 10:21

1 Answers1

6

The first character after s is used as the separator for the parameters to s, so this replaces anything matching ^[^.]+\. with the empty string. Traditionally this would be written

sed -r 's/^[^.]+\.//'
Stephen Kitt
  • 434,908
  • 4
    The reason to change the delimiter is to prevent the leaning toothpick problem: https://en.wikipedia.org/wiki/Leaning_toothpick_syndrome#Sed – bodgit Sep 02 '16 at 10:14