How to Edit File Using sed in Place Command in Linux?

Linux “sed” command line utility is a “stream editor” that performs editing operations on text files through a one-line command. It performs this task directly through the terminal however the user does not have to manually open and edit them on the text editor. It provides various features like search, replace, insert, delete, and substitute to manage and edit the files. It is mostly used to perform a “search and replace” operation on text files to quickly find out the specific text and replace it with the desired one.

The “sed” command allows the users to perform all the above operations on a file both on a temporary(without affecting the original file) and a permanent basis. By default, it makes the changes in the specified file at run time(temporarily) and does change the actual content of the file. However, the user can perform this task permanently with the help of its “in place” supported option.

Quick Outline

  1. How “sed” Command Edit Files?
  2. What is the “sed in Place” Command in Linux?
  3. How to Edit File Using sed in Place Command in Linux?
  4. What are the Alternatives to the “sed in place” Command in Linux?
  5. What is the Difference Between Linux “sed –in-place[SUFFIX]” and “tee” Commands?

This post will edit the files using the sed in place command in Linux.

How “sed” Command Edit Files?

The “sed” command takes the input(file/line) as a  stream and performs different operations on it to edit them. The working of this command relies on its generalized syntax which is stated here:

sed OPTIONS... [COMMAND] [INPUTFILE...]

Let’s break the above-stated “sed” command syntax:

  • OPTIONS: It denotes the supported options of the “sed” command.
  • COMMAND: It specifies the subcommands that may be the search pattern, flags, replacement strings, pattern-matching regexp, and much more. All these commands are separated by the pipe (|), backslash (\), or the forward-slash (/) delimiter.
  • INPUTFILE: It represents the input file name on which the editing operation needs to be performed.

Now, move on to the sed in place command.

What is the “sed in Place” Command in Linux?

Apart from the primary functionality, the “sed” command also offers a wide range of supported options to manage and edit files based on the user’s requirements. Among these options, there is a “-i” or “–in-place” option that makes new changes to the original file with or without creating its backup. This option updates the original file content instantly after its execution along with the “sed” command. 

Access “sed” Command Supported Options

All the supported options of the “sed” command can be easily accessed through the below-stated “–help” command:

sed --help

It can be analyzed that the execution of the above command provides “sed” generalized syntax and the list of supported options including “-i[SUFFIX]/ –in place[=SUFFIX]”:

How to Edit File Using sed in Place Command in Linux?

As the name implies the “sed” command line utility edits the text files to make their content accurate and updated. To achieve this functionality, use the below-stated syntax of the “sed” command:

sed -i[SUFFIX]/--in-place[=SUFFIX] [COMMANDS] [INPUTFILE...]

According to the above syntax, the “sed” command followed by the “–in-place” supported option edits the [INPUTFILE…] based on the specified [COMMANDS]. The explanation of the “[SUFFIX]” keyword is explained below.

  • The “SUFFIX” keyword provides additional “backup” functionality. It creates a copy of the original file as a backup to keep the original content safe and apply changes to the existing input file. 
  • The backup filename may get the prefix or suffix of the original file like “demo.txt.bkp” or “bkp.demo.txt”. Both the specified ways are valid. It depends on the user’s choice to use the backup filename as a prefix or suffix of the original file.
  • However, if the user specifies the “[SUFFIX]” along with the “–in-place” or “-i” flag itself then the backup file having the “[SUFFIX]” keyword will create prepending with the original filename.

Now use the above-stated sed inplace command syntax to edit files as per requirements.

Sample File

First, have a look at the sample file using the cat command

cat demo.txt

Note: The above sample file is used in all the below-stated examples of the sed in place command to perform different editing operations on it.

Example 1: Use the sed in Place Command to Find and Replace Specific Text From Files Without Backup

This example uses the “sed” command along with the in-place option to find and replace a specific text line from single and multiple files without creating their backups:

Single File Use Case

First start with the “single file” use case:

sed --in-place 's/DEBIAN/debian/g' demo.txtcat demo.txt

In the above “sed in place” command, the “s(substitute)” option matches the specified text string from the input file and replaces it with the given text “g(globally)”. Moreover, the “cat” command reads the “demo.txt” file content after modification.

Verification

It can be analyzed that the sed in-place command replaced the matched string and modified the original file content:

Multiple Files Use Case

Now use the “sed” command along with the “–in-place” flag to search and replace the specified text in multiple files having the “.txt” extension:

sed --in-place 's/Linux/itsLinuxFOSS/g'  *.txt

In the above command the “*(asterisk)” denotes all the files that have “.txt” extensions.

Output

The successful execution of the above command ensures that all the files having “.txt” extensions have been modified with the specified text globally:

Verification

For more verification use the “cat” command again to check whether each file content is modified or not:

cat notes.txt
cat sample.txt
cat myFile.txt

It can be analyzed that the specified string has been replaced with the new one in all text files:

Example 2: Use the sed in Place Command to Search and Remove Specific Text From a File With the Suffix Backup Name

This example uses the sed in-place command to search and remove specific text from a specific line and creates the backup of original content:

sed --in-place='.bkp' '/FEDORA/,+2d' demo.txtls

In the above command:

  • The “.bkp” represents the backup file extension prepending with the original file name as a suffix. It makes a copy of the original file to keep it as a backup.
  • The “d” command deletes “2” lines that come after the given string in the “demo.txt” file:
  • The “ls(list)” command lists the content of the current directory to check whether the backup file is created in the same directory or not.

Output

After the execution of the “sed” command the backup file prepending with the actual file name is successfully created in the same directory as the original file:

Verification

Now, execute the “cat” command to check the content of both the original and backup files:

cat demo.txt.bkp
cat demo.txt

It can be analyzed that the backup file contains a copy of the original file content. The input file contains the modified content in which “FEDORA” and its next two lines have been deleted successfully:

Example 3: Use the sed in Place Command to Insert New Lines in a File With Prefix Backup Name

This example utilizes the sed in place command to insert new lines in a file and creates a backup file whose name is now placed as a prefix of the original file name:

sed --in-place='backup.*' 's/UBUNTU/&\n\n\n\n/' demo.txtls

In the above-stated “sed” command:

  • The “backup.*” denotes the backup file name in which the “*” will replace the original file name.
  • The “s” command substitutes the specified number of new lines “\n” after the searched string. 

Output

The below output shows the successful execution of the “sed” command and confirms that the backup file as a prefix of the original file name has been created successfully:

Verification

Execute the “cat” command to see the content of both the actual and backup files for confirmation:

cat demo.txt
cat backup.demo.txt

It can be observed that the actual file has been modified having the addition of new lines after the matched string “UBUNTU”:

Example 4: Use the sed in Place Command to Print the Line Number and Save the Backup in a Different Directory

This example applies the sed in place command to print the lines and saves the input file backup in a different directory:

sudo sed --in-place='Downloads/*' '=' demo.txtls Downloads

In the above command:

  • The “Downloads/” refers to the directory where the user wants to create the backup of the input file and the “*” denotes the subdirectory path(if it exists).
  • The “=” specifies the line number before every line of the file.

Output

It can be observed that the sed in place command executes successfully and creates a backup file of the same name(original file) in the specified directory:

Verification

Now, for verification use the “cat” command to see the content of both the actual and input files:

cat demo.txt
cat 'Downloads/demo.txt'

The output shows that the “input file” placed in the “source” directory has been updated printing the line number in a file. Moreover, it also displays the “backup” file placed in the “specified(Downloads)” directory containing the original file content:

Example 5: Use sed in Place Command to Substitute Text If There is no Match Without Backup

This example uses the “sed” command with the “ in place” option to substitute the text string if there is no match without creating the backup of the original file:

sed --in-place '/FEDORA/! s/debian/FEDORA/' demo.txt
cat demo.txt

In the above “sed” command, the “!(Exclamation mark)” checks that if there is no match of the given pattern then substitute(s) the specified one at the place of the mentioned string in a file:

Output + Verification

The given string has been substituted in the place of existing one in the original file:

What are the Alternatives to the “sed in place” Command in Linux?

The functionality of the “sed” command along with the “–in-place” supported option can be achieved with some other Linux commands that are listed below:

  • Linux Perl command
  • Linux Tee Command

First start with the basics of the “Perl” command.

Alternative 1: Perl Command

The easiest and better replacement for the “sed” is the “Perl (Practical Extraction and Report Language)” command. It is an interpreter of the Perl programming language that scans the text file, retrieves specific information, and displays the results based on that content. 

Like the “sed” command, “Perl” also supports the “-i[extension]” supported option that edits files in places and saves the original content in the newly created file as a backup. Here is its practical implementation:

perl -pi'*.bkp' -e 's/UBUNTU/ubuntu/g' demo.txtls

In the above code block:

  • The “p” prints the same line as the “-n” option of the “sed” command.
  • The “-i” option saves the changes in the original file and creates a backup file of the “.bkp” extension prepending with the original filename. In addition, the user can get the name of the backup file as a prefix like the “sed” command.
  • The “-e” option defines the Perl code as it will appear through the command line.
  • The “ls” command displays the current working directory content.

Output

The above commands executed successfully showing both the original and the newly created backup file in the terminal: 

Verification

Lastly, for verification run the below-stated “cat” commands:

cat demo.txt
cat demo.txt.bkp

It can be observed that the “Perl -i” modified the original file by making changes in it and saved the original content in the newly created backup file:

Alternative 2: Linux Tee command

Linux offers another built-in tee command that takes the standard input from the execution of the concatenated command. After that,  it displays the output on the terminal or saves it into a new file. The concatenated commands are separated by the “|(Pipe)” character. This character executes them one by one after their successful execution and redirects their outputs to the next specified command or file.

What is the Difference Between Linux “sed –in-place[SUFFIX]” and “tee” Commands?

The “tee” command works the same as the “sed in place” command but with a little bit of difference, which is stated here:

  • The “tee” command saves changes in the new file and keeps the original file as a backup. Whereas the “sed” command followed by the “–in-place[SUFFIX]” option makes changes in the original file and keeps the new file as a backup”.

The below command shows its practical implementation:

cat demo.txt | sed 's/FEDORA/Arch/g' | tee Backup.txtls

In the above code block:

  • The “cat” command reads the content of the specified file.
  • The “sed” command searches the given pattern and replaces it with the newly specified string
  • The “tee” command redirects the output of the “sed” command to a new file and displays its content on the terminal.  

Output

The below output shows the successful execution of the specified commands and verifies that a backup file has been created in the source directory where the original file is saved:

Verification 

Now, run the below-stated “cat” commands to check the content of the backup and original file:

cat Backup.txt
cat demo.txt

It can be observed that the original file remained the same and the changes have occurred in the new file named “backup.txt”:

That’s all about editing files using the sed in place command.

Conclusion

The “sed” command followed by the “–in-place” option makes the changes in files and updates the existing content with the new one. It also provides an additional backup functionality to keep the original content safe and secure in a new file via the “[SUFFIX]” keyword. Apart from specifying the full form of “–in-place”, the user can also mention it in the shorter form “-i”. 

Furthermore, Linux also offers the “tee” command, and the “perl” followed by the “-i[extension]” flag to achieve the similar functionality performed by the “sed in place” command. This post has edited files using the sed in place command in Linux.