Reputation: 24795
Consider this line:
--dump-config=h264_sss-l2-2-ghb-16-500.ini --stats-file=h264_sss-l2-2-ghb-16-500.stat configs/example/cmp.py --l2cache -b h264_sss
and this string "l2-2-ghb-16". To change that string with SED, I ran this command:
sed 's/l2-.*-.*-.*-/l2-2-ghb-8-m-/g'
But the whole line then changed to
--dump-config=h264_sss-l2-2-ghb-8-m-b h264_sss
What is the problem
Upvotes: 1
Views: 173
Reputation: 882596
That regex is greedy inasmuch as .*
will match the maximum number of characters.
That means it will attempt to stretch the match from what you think is the first pattern all the way to what you think is the second.
While you may think there are two matched patterns, the fact that this stretching is happening means that there's only one, and it's longer than you think.
A quick fix is to ensure that it doesn't match beyond the next -
character with something like:
sed 's/l2-[^-]*-[^-]*-[^-]*-/l2-2-ghb-8-m-/g'
as per the following transcript:
pax> echo '--dump-config=h264_sss-l2-2-ghb-16-500.ini --stats-file=h264_sss-l2-2-ghb-16-500.stat configs/example/cmp.py --l2cache -b h264_sss' | sed 's/l2-[^-]*-[^-]*-[^-]*-/l2-2-ghb-8-m-/g'
--dump-config=h264_sss-l2-2-ghb-8-m-500.ini --stats-file=h264_sss-l2-2-ghb-8-m-500.stat configs/example/cmp.py --l2cache -b h264_sss
(command and output are slightly modified, lined up so you can easily see the transformations).
This works because, while .*
says the largest field of any characters, [^-]*
says the largest field of any characters except -
.
Upvotes: 2
Reputation: 426
The .*
portion matches the longest possible stretch of characters it can to make the pattern work. So the first .*
doesn't match just 2
as you hope, but 2-ghb-16-500.ini --stats-file=h264_sss-l2-2-ghb-16
, and so on. To make it work replace the dot's with [^-]
(any non-dash character). So,
sed 's/l2-[^-]*-[^-]*-[^-]*-/l2-2-ghb-8-m-/g'
Upvotes: 4
Reputation: 6790
sed looks for the most possible match. So -.*-
will match a string as large as possible.
Upvotes: 0