Imagine the following data stored in file data.txt
1, StringString, AnotherString 545
I want to replace "StringString" with "Strung" with the following code
sed -ir 's/String+/Strung/g' data.txt
But it won't work. This works though:
sed -ri 's/String+/Strung/g' data.txt
I don't see any reason why the order of option flags would matter. Is it a bug or is there an explanation?
Please note that I'm not looking for a workaround but rather why the order of -ir
and -ri
matters.
Sidenotes: The switch -i
"edits the file in place" while -r
allows "extended regular expression" (allowing the +
operator). I'm running sed 4.2.1 Dec. 2010 on Ubuntu 12.10.
String+
meansStrin(g)+
. If you want to match one or more occurrences of "String", use(String)+
, but that replaces "AnotherString" with "AnotherStrung", which I don't think is what you want. So, if you want to match exactly two occurrences, use(String){2}
, or if you want to match two or more, use(String){2,}
.