Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't `\d` work in regular expressions in sed? [duplicate]

Tags:

regex

linux

sed

awk

I am trying to use \d in regex in sed but it doesn't work:

sed -re 's/\d+//g' 

But this is working:

sed -re 's/[0-9]+//g' 
like image 346
user2036880 Avatar asked Feb 03 '13 09:02

user2036880


People also ask

What is D in sed?

It means that sed will read the next line and start processing it. Your test script doesn't do what you think. It matches the empty lines and applies the delete command to them.

Does sed support regular expressions?

A regular expression is a string that can be used to describe several sequences of characters. Regular expressions are used by several different Unix commands, including ed, sed, awk, grep, and to a more limited extent, vi.

How do you make sed not greedy?

sed does not support "non greedy" operator. You have to use "[]" operator to exclude "/" from match. P.S. there is no need to backslash "/".

What is r option in sed command?

r is used to read a file and append it at the current point. The point in your example is the address /EOF/ which means this script will find the line containing EOF and then append the file specified by $thingToAdd after that point. Then it will process the rest of the file.


2 Answers

\d is a switch not a regular expression macro. If you want to use some predefined "constant" instead of [0-9] expression just try run this code:

s/[[:digit:]]+//g 
like image 125
Kamil Avatar answered Nov 04 '22 08:11

Kamil


There is no such special character group in sed. You will have to use [0-9].

In GNU sed, \d introduces a decimal character code of one to three digits in the range 0-255. As indicated in this comment.

like image 28
Ivaylo Strandjev Avatar answered Nov 04 '22 08:11

Ivaylo Strandjev