My command is like this:
echo "12 cats" | sed 's/[0-9]+/Number/g'
(I'm using the sed
in vanilla Mac)
I expect the result to be:
Number cats
However, the real result is:
12 cats
Does anyone have ideas about this? Thanks!
Because you are using PCRE (Perl Compatible Regular Expressions) syntax and sed doesn't understand that, it uses Basic Regular Expressions (BRE) by default. It knows neither \s nor \d .
Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.
The sed command, short for stream editor, performs editing operations on text coming from standard input or a file. sed edits line-by-line and in a non-interactive way. This means that you make all of the editing decisions as you are calling the command, and sed executes the directions automatically.
+
must be backslashed to get its special meaning.
echo "12 cats" | sed 's/[0-9]\+/Number/g'
Expanding the +
modifier works for me:
echo "12 cats" | sed 's/[0-9][0-9]*/Number/g'
Also, the -E
switch would make the +
modifier work, see choroba’s answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With