Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

sed

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 ...


Imagine the following data stored in file data.txt

1, StringString, AnotherString 545
2, StringString, AnotherString 723
3, StringString, AnotherString 610
4, StringString, AnotherString 118
5, StringString, AnotherString 482

Desired string transformation

Replace "StringString" with "Strung"

With the following code

sed -ir 's/String+/Strung/g' data.txt <-- won't work

sed -ri 's/String+/Strung/g' data.txt <-- will work

I don't see any reason why the order of option flags would matter? Any explanations?

Sidenotes: The switch -i "edits the file in place" while -r allows "extended regular expression" (allowing the + operator). Running sed 4.2.1 Dec. 2010 on Ubuntu 12.10.

like image 900
somethis Avatar asked Jul 08 '13 11:07

somethis


People also ask

What is the flag in sed?

The select flag permits an executable to request and be part of SED protection during the select mode of systemwide SED operation, whereas the exempt flag permits an executable to request for an exemption from the SED mechanism. These executables are not enabled for execution disable on any of the process memory areas.

What is r option in sed command?

r is used to read a file and append it at the current point. The point in your example is the address /EOF/ which means this script will find the line containing EOF and then append the file specified by $thingToAdd after that point. Then it will process the rest of the file.

What is sed pattern?

Sed reads the input stream until it finds the new line character \n . Then it will take all the data it read so far and place it into a buffer without newline character \n . Most of the commands operate on this data present in the buffer. This buffer is known as pattern buffer.


1 Answers

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

like image 155
Andreas Wederbrand Avatar answered Oct 02 '22 21:10

Andreas Wederbrand