sed Command – Remove Everything After FQDN Dot

regular expressionsed

I am new to sed and am having some troubles making it work.

What I want is this:

abc.ztx.com. A 132.123.12.44 ---> abc.ztx.com

I used the below pattern, but doesn't seem to work:

echo "abc.ztx.com. A 132.123.12.44" | sed 's/\.\s.+//g'

I verified the regex using regex101.com and pattern, \.\s.+ matches the part . A 132.123.12.44 perfectly. Why is it not working with sed.

Appreciate your help. Thank you.

Best Answer

sed uses POSIX basic regular expressions (BRE) by default. \s is a PCRE (Perl-compatible regular expression) which is equivalent to the BRE [[:blank:]] (I think, matching spaces and tabs, or possiby [[:space:]] which matches a larger set of whitespace characters). The + is a POSIX extended regular expression (ERE) modifier, which is equivalent to \{1,\} as a BRE.

So try

sed 's/\.[[:blank:]].*//'

instead. You may replace [[:blank:]] by a space character if you don't need to match tabs:

sed 's/\. .*//'

Note that there is no need to do the substitution with the g flag as there will only ever be a single match. Also, the .+ that you use could just be replaced by .* instead of .\{1,\} as we don't care whether there are any further characters at all (just delete all of them).

Related:

Related Question