There is an example in this link about sed:
To delete the first number on all lines that start with a "#" use:
sed '/^#/ s/[0-9][0-9]*//'
What is the benefit of first pattern(/^#/)? It could be simply:
sed 's/^#[0-9][0-9]*//'
There is an example in this link about sed:
To delete the first number on all lines that start with a "#" use:
sed '/^#/ s/[0-9][0-9]*//'
What is the benefit of first pattern(/^#/)? It could be simply:
sed 's/^#[0-9][0-9]*//'
The general format of sed commands is
[address[,address]] function
When a command has a single address, it operates on all lines that match that address. When a command has no address, it operates on every single line.
Reference: POSIX sed
Regarding your specific examples:
/^#/ s/[0-9][0-9]*//
This command has an address, /^#/, which matches all lines beginning with a #.
The substitution pattern is /[0-9][0-9]*/. This matches the first sequence of digits wherever it occurs in the line.
Plain English summary: delete the first sequence of digits in every line beginning with a #.
Example: # non-digits|5555|non-digits|5555 becomes # non-digits||non-digits|5555
s/^#[0-9][0-9]*//
There is no address, so this command operates on every single line.
The substitution pattern, /^#[0-9][0-9]*/, matches a sequence of consecutive digits preceded by a # anchored at the beginning of the line.
Plain English summary: delete # followed by a sequence of digits (and only that pattern) from the beginning of every line.
Example: #5555|non-digits|5555 becomes |non-digits|5555, but
# non-digits|5555|non-digits|5555 is unchanged because the substitution
pattern does not match.
The first will match and substitute:
#abc99
The second will not.
Plus, the second will also remove the initial #.
[0-9][0-9]*why not[0-9]\+?\+is guaranteed by POSIX.\+is in POSIX extended regular expressions. en.wikipedia.org/wiki/Regular_expression#Syntaxseduses BRE's though.-r, other times with flag-E), and there is talk of adding this capacity to the POSIX standard forsed. @jw013 is correct though that the current POSIX standard doesn't requiresedto handle anything other than BREs. EREs handle plain+; somesedimplementations enhance their BREs to also handle\+, but if I remember rightly, this is not part of POSIX. Instead ofp\+you could usep\{1,\}, which is a POSIX BRE.