The “sed” (Stream Editor) is the command in Linux that is considered for finding, replacing, inserting and deleting the text of the string. This command is mostly used for finding and replacing text in the strings. Multiple edits are useful in reducing the time for modification of the files when the user wants to display the output with a little different text.
“sed” is a powerful tool that can fulfill several text-processing tasks. One of the common use cases of “sed” is to make multiple edits to a file or a stream of text with a single call. It saves resources as well as time, particularly when dealing with large files or complex patterns.
This post will demonstrate the various examples of the “sed” command in Linux to make multiple edits in a file.
- Why Need to Make Multiple Edits With a Single Call to sed?
- Working of sed Command
- How to Make Multiple Edits Using the sed Command? (single Call to sed)
- Customize Text Processing Tasks Using sed Command
Why Need to Make Multiple Edits With a Single Call to sed?
The “sed” command manipulates text files in Linux. It can complete several tasks on the input, like search and replace, append, insert, delete, and many more. One of the features of sed is that it can apply multiple commands to the input with a single call.
Working of sed Command
There are different ways to make multiple edits with sed, depending on the syntax and the operating system.
The basic syntax of the “sed” command in Linux is as below:
sed [options] 'command' file
In the above syntax,
- “options” are optional flags that modify the behavior of sed,
- “command” is a single instruction that tells “sed” what to do,
- “file” is the name of the file to process.
Note: If no file is specified, “sed” reads from the standard input.
How to Make Multiple Edits Using the sed Command? (single Call to sed)
Multiple edits can be performed on a string and the files using the “sed” command. To perform multiple edits on any string with a single call, use the following command syntax:
Syntax
sed -e 's/[text to change]/[text to replace]/g' -e 's/[text to change]/[[text to replace]]/g'...so on
The description of the above syntax is given below:
- The “sed” to apply the multiple edits.
- Use the “e” flag script/expression
- Enter the available text in the “text to change” section for replacement.
- Enter the replacing text in the “text to replace” section.
- The “g” in the syntax represents a global.
- Repeat the same pattern separated by the “e” flag according to the requirement.
Let’s discuss with the first example.
Example 1: Multiple Edits With a Single Call in a String
To apply the multiple edits on the string, use the “echo” statement and pass it to the “sed” command using the above syntax. The following command will replace the text “Multiple” with “text” and “Edits” with “Edited”:
echo "sed Command Multiple Edits" | sed -e 's/Multiple/text/g' -e 's/Edits/Edited/g'
The strings have been replaced with the given text “Multiple” with “text” and “Edits” with “Edited”.
Example 2: Multiple Edits With a Single Call in a File
Likewise, the user can apply multiple edits through the “sed” command on any file with the following syntax:
sed -e 's/[text to change]/[text to replace]/g' -e 's/[text to change]/[[text to replace]]/g'..... <File Name>
The description of the above syntax is given below:
- The “sed” to apply the multiple edits.
- Use the “e” flag script/expression
- Define “text to change” and “text to replace” in the pattern
- Repeat the same pattern separated by the “e” flag.
- In the end, “File Name” describes the file name to apply the sed pattern.
Let’s create a sample file named “file.txt” using nano editor and place some text on it. For instance, the following file “file.txt” is displayed used the “cat” command:
cat file.txt
The content of “file.txt” is displayed in the terminal above.
The text in the “file.txt” will change with the “Johnson” with “Johnson-User” and “site” with “blog”:
sed -e 's/Johnson/Johnson-User/g' -e 's/site/blog/g' file.txt
The text is changed as shown above. Here, multiple -e options to mention multiple commands.
Example 3: Multiple Edits With a Single Call to Another File
Suppose the user applies the same “sed” command pattern on the files. For this, the script needs to be stored in a file. In this way, that script can be applied to target files to obtain the output.
You can create a file with one command per line, and then use the -f option to tell sed to read the commands from the file.
Let’s store the sed patterns in a new file named “command.txt” file as shown below:
sudo nano command.txt
The sed pattern in a file called “command.txt” is as below:
s/Johnson/Johnson-User/g
s/site/blog/g
The sed patterns are stored in the “command.txt” file. Now, save and close the text editor.
To apply it on the target file, use the “sed” command with the “f” flag. After that, define the file name such as “command.txt” and the target file such as “file.txt”:
sed -f command.txt file.txt
The file “file.txt” output is replaced with text defined in the “command.txt” file.
Note: Use a script file to store multiple commands. It is beneficial if users have a complex or long set of commands that they want to reuse or document.
Example 4: Multiple Edits Using Multiple sed Commands (Pipeline)
The user can also apply multiple edits using the multiple “sed” commands. For this, define the sed command pattern and file name “file.txt,” then pass it to another sed command as input through the pipe command:
sed 's/Johnson/Johnson-foss/g' file.txt | sed 's/itslinuxfoss/itslinuxfoss.com/g'
The above command has made the following changes in the file’s content, “Johnson” with “Johnson-foss” and “itslinuxfoss” with “itslinuxfoss.com.”
Example 5: Multiple Edits Using Single sed Command (Semi-colon)
To separate multiple commands, use semicolons between them. For example, to replace all occurrences of “Johnson” with “Roger” and “itslinuxfoss” with “ILF” in a file, use the sed command as below:
sed 's/Johnson/Roger/g; s/itslinuxfoss/ILF/g' file.txt
Here, “Johnson” is replaced with “Roger” and “itslinuxfoss” with “ILF”.
Customize Text Processing Tasks Using sed Command
These are some of the methods to make multiple edits with a single call to sed. You can also combine them as needed, or use other options and features of sed to customize your text processing tasks. For this, explore the below content.
Read Capture Group using sed Command
Read Edit File Using sed in Place Command
Read Delete a Line Using sed Command
Read Replace String in a File Using sed Command
Read sed Command | sed ‘s/\s\s*/ /g’
Read Remove White Spaces Using sed Command
That is all from sed command practical implementation.
Conclusion
In Linux, multiple edits with a single call through the “sed” command can be performed on a string or a file by repeating the multiple sed patterns separated by the “e” flag. The sed patterns can be stored in a file, and that file can be applied using the “f” flag to the target file to replace the given text.
The sed is a powerful tool for manipulating text files and performing multiple edits with a single call by using the -e option. It also allows users to specify multiple commands separated by semicolons. This write-up has illustrated the sed command to apply multiple edits using various examples.