beppler
February 24, 2009, 2:59pm
1
I need to replace the numbers with a new string.
How can I give a wildcard for the different # of numbers
sed '/abcdef/s/abcdef=*/abcdef=999999/'<foo>foo1
From: To:
abcdef=1234 abcdef=999999
abcdef=12345 abcdef=999999
abcdef=123456 abcdef=999999
abcdef=1234567 abcdef=999999
abcdef=12345678 abcdef=999999
abcdef=123456789 abcdef=999999
abcdef=1234567890 abcdef=999999
Thanks in advance!!
Brandt
sed '/abcdef/s/abcdef=[0-9]\+/abcdef=999999/' <foo >foo1
beppler
February 24, 2009, 3:22pm
3
With the single [0-9] I don't pick up any lines. That is why I placed 4 [0-9] in a row, but I don't allways have 4 digits in number it would be unknown..
Thanks
Brandt
That's why I put \+ after [0-9]; it means one or more of the preceding character.
beppler
February 24, 2009, 3:51pm
5
That is what I thought it ment but does not seem to work!! Solaris v10
Thanks
Try it without the backslash.
Then use [0-9]* or [0-9][0-9]*.
echo "abcdef=123456" | sed 's/=[0-9]*$/=999999/'
rikxik
February 26, 2009, 12:02am
10
Try the longhand for +
$ cat foo
abcdef=
abcdef=1234
abcdef=12345
abcdef=123456
abcdef=1234567
abcdef=12345678
abcdef=123456789
$ sed '/abcdef/s/abcdef=[0-9]\{1,\}/abcdef=999999/' foo
abcdef=
abcdef=999999
abcdef=999999
abcdef=999999
abcdef=999999
abcdef=999999
abcdef=999999
rikxik
February 26, 2009, 12:04am
11
That will also add 999999 to cases where there is no number after abcdef=
$ echo "abcdef=" |sed 's/=[0-9]*$/=999999/'
abcdef=999999
That may not be desired (or may be...).