Wednesday, October 5, 2022

[SOLVED] Sed command - order of option flags matters? (-ir vs -ri)

Issue

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.


Solution

When doing -ir you are specifying that "r" should be the suffix for the backup file.

You should be able to do -i -r if you need them in that order



Answered By - Andreas Wederbrand
Answer Checked By - Cary Denson (WPSolving Admin)