166

I'm trying to use sed to substitute all the patterns with digits followed immediately by a dot (such as 3., 355.) by an empty string. So I try:

sed 's/\d+\.//g' file.txt

But it doesn't work. Why is that?

kenorb
  • 25,417
Mika H.
  • 1,849

4 Answers4

226

Because sed is not perl -- sed regexes do not have a \d shorthand:

sed 's/[[:digit:]]\+\.//g'

sed regular expression documentation here.

glenn jackman
  • 26,306
  • 6
    That's if you want zero or more digits. /[[:digit:]]*\. / will match the string foo. because you allow for zero digits. If you want one or more use \+ as shown – glenn jackman Mar 02 '16 at 11:19
127

Two problems:

  1. sed does not support \d. Use [0-9] or [[:digit:]].

  2. + must be backslashed to get the special meaning: \+.

choroba
  • 19,261
  • 4
  • 49
  • 52
  • 18
    instead of backslashing the + (which doesn't work on Mac OS X, for example) you can use the -E option to interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's). Sadly, this doesn't help with the \d issue... – gMale Aug 06 '13 at 01:01
  • 7
    @gmale: -E does not work for GNU sed, it uses -r instead. – choroba Aug 06 '13 at 06:54
  • 26
    Okay... it sure looks like sed just sucks when it comes to portability... – iconoclast Jul 14 '14 at 15:16
  • @choroba I think I confused myself today. I'm going to delete my noisy comments... – Steven Lu Jun 28 '18 at 21:47
  • 2
    Basically, what I was trying to warn people about is: If you do not use extended regex (-E on BSD sed and -r on GNU sed), in BSD sed, neither + nor \+ (same with ?) will work at all, whereas in GNU sed you can get them to work with the backslash. Hence the common recommendation to use extended regex in scripting – Steven Lu Jun 28 '18 at 21:55
  • The question of whether -E can be relied upon in linux to do the same as -r is what's unclear to me now. – Steven Lu Jun 28 '18 at 22:04
  • I'd just use Perl :-) – choroba Jun 28 '18 at 22:12
  • When you are in a real hurry and all you can find are posts like this, but they never explain the meaning of , or +. – Owl Apr 13 '22 at 14:53
  • At the risk of sounding patronising, if you don't know what + does, you don't need SE but a tutorial for basics – BIOStheZerg Jul 20 '22 at 09:27
13

Adding to the other answers a few years later, I found I wanted the extended feature for a more complex regex

This expects simply + for one or more, and generally made the string more obvious to me for both my case and this one

# NOTE \d is not supported
sed --regexp-extended 's/[0-9]+\.//g'

-E -r --regexp-extended are all the same

Using sed 4.7

ti7
  • 256
7

The sed man page references the re_format man page. It makes 2 distinctions: (1) obsolete versus extended regular expressions; (2) non-enhanced versus enhanced regular expressions. All 4 combinations are possible. There is support in sed for both obsolete and extended, but in either case only for non-enhanced. The \d operator is a feature of enhanced regular expressions, therefore not supported by sed.