I want to replaced all number with '@' symbol. I am using the below sed command , but not getting the desired result.
command -
echo "abc 434 pankaj 444" | sed 's/[0-9]*/@/g'
Run Code Online (Sandbox Code Playgroud)
Result -
@a@b@c@ @ @p@a@n@k@a@j@ @
Run Code Online (Sandbox Code Playgroud)
Well, quite simply, [0-9]*
matches strings that consist entirely of zero or more digits, include empty strings. Anything that matches an empty string, matches between any two characters, so the replacement @
is added between all letters in the input. Strings of multiple digits are replaced with one @
since the expression matches all consecutive digits at once.
So in the input string ab43
the matches to [0-9]+
are (with some whitespace added for clarity):
a b 434
^ ^ ^^^- here, a string of some digits
^ ^- here, a zero-length string
^- here, a zero-length string
Run Code Online (Sandbox Code Playgroud)
Use [0-9]
to match exactly one digit, or [0-9][0-9]*
to match one or more (or [0-9]+
in extended regular expressions).