Using Sed to Replace Line Feed
Introduction of Linux Sed Replace
In the Linux operating system, the sed command is used to filter the text data from the input file and transform the text data. The sed is known as the stream editor. Majorly, the sed command will perform the multiple operations on the input file like text searching, deletion, find and replace the text data, data/text insertion, etc. The main advantage of the sed command, without open the file, we are able to replace the text data. Similar to the sed command, we are having different option to replace the text data like awk, etc.
The sed command utility was written by Jay Fenlason, Tom Lord, Ken Pizzini, and Paolo Bonzini.
Syntax:
Syntax of Sed Replace Command
sed [OPTION]... { script (only-if-no-other-script) } [input-file]...
- sed: We can use the "sed" keyword in the syntax or command. It will take different arguments like an option, script file, or input file. As per the provided arguments, it will do the filtering process and data transformation on it.
- OPTION: We can provide the different flags as options that are compatible with the "sed" command.
- script: We can provide the script file as an input to the sed command to perform the necessary transformation on it.
- Input-file: We can provide the input-file as an input to the sed command to perform the necessary text/data transformation operation on it.
How Linux Sed Replace Command Works?
The Linux environment, we are working on multiple scripts or files like backup, application development, etc. If we need to do any manual changes in the script. It is a very tedious and complex job. To overcome this condition, we need to use the "Linux sed replace" command. The command helps to transform the data.
The sed command will accept the different inputs like the sed option, input file, or the script. As per the giving sed option and input_file, the sed will perform the data transformation operation on the input data or file.
The sed command considers the various options while data transformation. Below are the lists of options that are compatible with the sed command.
Sr No | Option | Description |
1 | -n, –quiet, –silent | it will help to suppress the automatic printing of pattern space |
2 | -e script, –expression=script | It will add the script to the commands to execute the input script |
3 | -f script-file, –file=script-file | It will add the contents of the script_file to the commands to be executed |
4 | –follow-symlinks | It will follow the symlinks when the processing in place |
5 | -i[SUFFIX], –in-place[=SUFFIX] | It will edit the file in place |
6 | -c, –copy | It will use copy action instead of the rename when shuffling files in the -i mode |
7 | -l N, –line-length=N | It will specify the desired line-wrap length in terms of the `l' command |
8 | –posix | It will disable all GNU extensions. |
9 | -r, –regexp-extended | It will use the extended regular expressions in the input script. |
10 | -s, –separate | It will consider the files as separate rather than a single continuous long stream. |
11 | -u, –unbuffered | It will load the minimal amount of data from the input files and flush the output buffers |
12 | -z, –null-data | It will separate the lines by NUL characters |
13 | –help | It will display this help of sed command and exit it |
14 | –version | it will hive the version information of the sed command |
Examples to Implement Linux Sed Replace Command
Following are the examples are given below:
Input File Contain
File Name: input_file.txt
File Contain:
Hello,
Welcome to the Linux Sed Tutorial
The sed tutorial is very simple to learn (sed command).
We can use the sed command for text transformation.
1. Replace the String
The "sed replace" command is a very simple and common way to replace the string in a Linux environment.
Command:
sed 's/sed/sed replace/' input_file.txt
cat input_file.txt
Explanation: We are replacing the first string value (sed) with the second string value (sed replace).
Output:
2. Replace String with "nth" Occurrence
By default, we are able to replace the string in the first occurrence. But in sed replace command, we are able to change the string at "nth" occurrence
Command:
sed 's/sed/sed replace/2' input_file.txt
cat input_file.txt
Explanation: As per the above command, we are changing the string at second occurrence in a single line.
Output:
3. Replace the String with all Occurrence
In the Sed Replacecommand, we are able to replace all the occurrences of the string.
Command:
sed 's/sed/sed replace/g' input_file.txt
cat input_file.txt
Explanation: As per the above command, we are replacing all the occurrences of the "sed" string with the "sed replace" string.
Output:
4. Replace the String with single"nth"Line Occurrence
In the Linux environment, we are having the sed replace functionality to change the specific occurrence of the string (within a single line).
Command:
sed 's/sed/sed replace/2g' input_file.txt
cat input_file.txt
Explanation: As an input file, we are having the 2 occurrences of the "sed" string. We are replacing only the second occurrence of the input file. In the 3rd line, we have found the 2nd occurrence then it was replaced from "sed" to "sed replace".
Output:
5. Replace the String on Specific Line
In the Sed Replace command, we can replace the string on a specific line only.
Command:
sed '3 s/sed/sed replace/' input_file.txt
cat input_file.txt
Explanation: We are replacing the string on the 3rd line only. It will be no impact on any other line even though there is a matching string as well.
Output:
6. Sed Replace Command – With "p" Flag
We are having the functionality, to print the line those are matching with the replace string. If the input string will not match then it will print only one time.
Command:
sed 's/sed/sed replace/p' input_file.txt
cat input_file.txt
Explanation: As per the above command, we are able to print the matching string line two times. The lines are not matching with the option string, it will print a single time.
Output:
7. Replace the String from Specific Range of Lines
In the Sed Replacecommand, we can replace the string from the specific range of the input file/lines.
Command:
sed '2,3 s/sed/sed replace/' input_file.txt
cat input_file.txt
Explanation: We are replacing the string in-between line number 2 to 3, except that the string replace operation will not work.
Output:
Conclusion
We have seen the uncut concept of "Linux Sed ReplaceCommand" with the proper example, explanation and command with different outputs. The Sed Replacecommand is used for the text fileting and transformation. It is very useful in the shell and application-level jobs for replacing the string.
Recommended Articles
This is a guide to Linux Sed Replace. Here we also discuss the introduction and how linux sed replace command works? along with different examples and its code implementation. You may also have a look at the following articles to learn more –
- Linux Shutdown Command
- Linux WC
- Linux bg Command
- Linux Sleep
Source: https://www.educba.com/linux-sed-replace/
Post a Comment for "Using Sed to Replace Line Feed"