I need to manipulate text that's in a file.
I need to append a right parenthesis – )
– after every number
that's preceded by a comma – ,
– e.g.,
,4
➜,4)
,15
➜,15)
I am struggling to find the proper way to do this with sed
. I tried the following, which works for 1 digit but can't seem to be extended to 2 digits (so it's fine for first line above but not the second):
sudo sed 's/,\([0-9]\)/,\1)/g' filename
So then I tried the following for 2 digits:
sudo sed 's/,\([0-9]([0-9]?\)/,\1)/g' filename
This didn't do anything - the file remained unchanged, though I did not receive an error message. What is the proper way to search for one required digit and a second optional digit and then move them both into the replacement text? I still need to accomplish this:
,15
➜,15)
Any advice would be greatly appreciated.
\{,2\}
is more syntaxically correct but you variant is workable too if your escape "question mark"\?
and remove extra-bracket(
between][
:sed 's/,[0-9][0-9]\?/&)/g' filename
sed 's/,[0-9]\{1,2\}/&)/g'
. @Costas,\?
is GNU specific. The portable/standard equivalent is\{0,1\}
.?
,+
,{
,|
,(
, and)
lose their special meaning; instead use the backslashed versions\?
,\+
,\{
,\|
,\(
, and\)
.\?
nor\+
.