Tuni
Tuni

Reputation: 129

Match string followed by any character followed by float number

I am trying to match bla = 0.05 and replace the number with 1234 in a file. Expected output is bla = 1234

Code I tried:

sed -i '' "s/\(bla\)\(.*\)\([-+]?[0-9]*\.?[0-9]*\)/#\1\21234/g" foo

Also, why do I sometimes need '' and sometimes not to call sed ?

Upvotes: 1

Views: 134

Answers (1)

Sundeep
Sundeep

Reputation: 23667

$ echo 'bla = 0.05' > foo
$ cat foo 
bla = 0.05
$ sed 's/\(bla[^0-9.+-]*\)\([-+]\?[0-9]*\.\?[0-9]*\)/\11234/g' foo
bla = 1234


If extended regex option is available, either -E or -r

$ sed -E 's/(bla[^0-9.+-]*)([-+]?[0-9]*\.?[0-9]*)/\11234/g' foo
bla = 1234


See sed in-place flag for requirements of using -i flag between different sed versions

Upvotes: 1

Related Questions