Issue
So I have a file that contains many instances of something like foo.
, foo..
, foo...
, and so on. I would like to replace them all with bar
.
The command I used was sed -i 's/foo\.+/bar/g' abc.c
. However, it seems like sed only matches the minimal string, so foo..
is replaced by bar.
, foo...
with bar..
and so on.
Is there a way to get sed to do what I want?
Solution
Of the 4 proposals in the comments, only half are correct, as follows:
Mac_3.2.57$cat abc.txt
0: foo
1: ifoo.
2: foo..
3: foo...
x: blah
inf: foo.........
Mac_3.2.57$#wrong-- matches "foo" followed by 0 or more "."s
Mac_3.2.57$sed 's/foo\.*/bar/g' abc.txt
0: bar
1: ibar
2: bar
3: bar
x: blah
inf: bar
Mac_3.2.57$#right-- matches "foo" followed by 1 or more "."s
Mac_3.2.57$sed -E 's/foo\.+/bar/g' abc.txt
0: foo
1: ibar
2: bar
3: bar
x: blah
inf: bar
Mac_3.2.57$#wrong-- matches "foo.+"
Mac_3.2.57$sed 's/foo\.\+/bar/g' abc.txt
0: foo
1: ifoo.
2: foo..
3: foo...
x: blah
inf: foo.........
Mac_3.2.57$#right-- matches "foo" followed by 1 or more "."s
Mac_3.2.57$sed -E 's/foo\.{1,}/bar/g' abc.txt
0: foo
1: ibar
2: bar
3: bar
x: blah
inf: bar
I would use what I consider the simplest: sed 's/foo..*/bar/g' abc.txt
Answered By - Andrew Answer Checked By - Cary Denson (WPSolving Admin)