What does the sed -i option do?

I am debugging a shell script and trying to figure out the task performed by the following command:

sed -i '1,+999d' /home/org_user/data.txt 

I need to change this command as its failure with the following error:

 illegal option sed -i 

But before the change, I need to understand the work of BAU.

Appreciate any inputs in this regard.

+6
source share
3 answers

The applicable use of this is as follows. Let's say you have the following file.txt file:

 1, 2, 6, 7, "p" 

We want to replace "p" with 0.

 sed 's/"p"/0/g' file.txt 

Using the above just prints the output on the command line.

You think, why not just redirect this text back to the file as follows:

 sed 's/"p"/0/g' file.txt > file.txt 

Unfortunately, due to the nature of the redirection above, an empty file will simply be created.

Instead, a temporary file should be created for output, which later overwrites the original file like this:

 sed 's/"p"/0/g' file.txt > tmp.txt && mv tmp.txt file.txt 

Instead of making a long workaround above the sed edit in place option with -i, you can use a much simpler command:

 sed -i 's/"p"/0/g' file.txt 
+8
source

If the -i option is specified, sed edit the files in place.

  -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied) 

from sed (1)

http://www.gnu.org/software/sed/manual/sed.html#Introduction

+4
source

Some sed implementations do not support the -i option. What he does can be modeled with

 sed -e '...' file > tmp mv tmp file 
+2
source

Source: https://habr.com/ru/post/952809/


All Articles