Sed Command in Linux/Unix with Examples
Linux 'sed' command stands for stream editor. It is used to edit streams (files) using regular expressions. But this editing is not permanent. It remains only in display, but in actual, file content remains the same.
Primarily, it is used for text substitution; additionally, it can be used for other text manipulation operations like insert, delete, search, and more. The sed command allows us to edit files without opening them. Regular expression support makes it a more powerful text manipulation tool.
Overview of sed command
It is a Unix utility that transforms and parses text with a compact and simple programming language. It was integrated from 1973 to 1974 by Lee E. McMahon of Bell Labs and is present today for almost every operating system. The sed command was based on the scripting aspects of the earlier qed (quick editor) and the ed interactive editor. It was the earliest tool for supporting regular expressions, and remains active for text processing, most importantly with the substitution command. Famous alternative tools include Perl and AWK for "stream editing" and plaintext string manipulation.
Operation Mode of sed Command
The sed command is a text processing line-oriented utility: it line by line reads text from a file or input stream into an internal buffer known as the pattern space. All line reads begin a cycle. The sed command uses one or multiple operations which have been described by a sed script to the pattern space. It operates a programming language using about 25 commands that describe the operations over the text.
For all input lines, the sed command ordinarily results in the pattern space and starts the cycle with the next line again after executing the script. Other end-of-script natures are available from script commands and set options, e.g., N to immediately include the next line into the pattern space, q to quit, d to remove the pattern space, and so on. Hence, a sed script relates to a loop body that iterates from a streamline, in which the loop variable and the loop itself are maintained and implicit by sed.
Usage of sed Command
The following are some command line options of the sed command:
-n, --quiet, --silent: It forcefully allows us to print of pattern space.
-e script, --expression=script: It is used to add the script to the commands to be executed.
-f script-file, --file=script-file: It is used to add the contents of script-file to the commands to be executed.
--follow-symlinks: it is used to follow symlinks when processing in place.
-i[SUFFIX], --in-place[=SUFFIX]: it is used to edit files in place (creates backup if SUFFIX option is supplied).
-l N, --line-length=N: It is used to specify the desired line-wrap length for the `l' command.
--posix: it is used to disable all GNU extensions.
-E, -r, --regexp-extended: It allows us to use the extended regular expressions in the script (for portability use POSIX -E).
-s, --separate: it is used for considering files as separate rather than as a single and continues the long stream.
--sandbox: It is used to operate in sandbox mode.
-u, --unbuffered: It is used for loading the minimal amounts of data from the input files and flushes the output buffers more often.
-z, --null-data: It is used to separate lines by NUL characters.
--help: it is used to display the help manual.
--version: It is used to display version information.
Examples of sed Command
Let's see the following examples:
Applying to the STDIN directory
The sed command is not just limited to manipulate files; also, we can apply it to the STDIN directory.
The above commands will replace the first text with the second text pattern. Consider the below output:
From the above output, first, we have performed 'sed' command on a string 'class7' where 'class' is changed into 'jtp' and 7 into 10. Then we have performed 'sed' command on a stream 'msg.txt' where 'learn' is converted into 'study.'
In the earlier example, all 'learn' words were not edited into 'study'. To edit every word, we have to use a global replacement 'g'. It will edit all the specified words in a file or string.
Consider the below examples:
The above commands will replace all the specified text pattern. Consider the below output:
From the above output, by executing the command "echo class7 class9 | sed 's/class/jtp/g'" all the 'class' is converted into 'jtp' and with command "cat msg.txt | sed 's/learn/study/g'" all the 'learn' was converted into 'study'.
Removing a Line
The 'd' option will let us remove a complete line from a file. We only need to specify a word from that line with 'd' option, and that line will be deleted. But, note that all the lines having that same word will be deleted. It will be executed as:
Consider the below command:
The above command will delete the lines having the word 'jtp'. Consider the below output:
From the above output, by executing the command "cat msg.txt | sed '/jtp/d'" all lines containing the word 'jtp' are deleted.
Using the Multiple sed Command
The '-e' option allows us to execute the multiple sed commands at once. We can perform more than one sed operation by executing the command as:
Consider the below command:
The above command will apply all the specified operations in file 'exm.txt'. Consider the below output:
As we can see from the above output, all the 'red' words are replaced with 'blue,' and all the 'yellow' words are replaced with 'black.' We can also separate commands like this:
The result will be the same as the above command.
Reading Commands From a File
We can save the sed commands in a file and apply them at once in any file. It can be done by specifying the '-f' option as follows:
From the above command, the '<sed file>' is a file that has a sed command list. Consider the below command:
The above command will apply all the specified commands in the 'SedCommand' file on 'exm.txt'. Consider the below output:
From the above output, we have used commands applied to the earlier example. So, the output is the same as the previous example.
We can use the exclamation mark (!) as a string delimiter. For example, we want to replace bash shell and replace it with csh shell in the "/etc/passwd". To do so, execute the below command:
We can achieve the same result by executing the below command:
Limiting the sed
The basic use of the sed command process the entire file. But, we can limit the sed command and specify any line. There are two ways to limit the sed command:
We can provide a number to specify a line as follows:
The above command will apply the specified operation on the third line. Consider the below output:
From the above output, only the line three is modified.
We can also specify a range of lines. To specify a range of lines, execute the command as follows:
The above command will update the specified text in lines 1 and 3. Consider the below output:
Inserting and Appending Text
The 'i' and 'a' flag is used to insert and append the text on a file. The 'i' flag will add the text before the string, and the 'a' flag is used to add text after the string. Consider the below command:
The above command will insert the text before the text "Another Demo". Consider the below output:
To append text, execute the command as follows:
The above command will append the text. Consider the below output:
The 'c' flag is used to modify a specific line. To modify a line, execute the command as follows:
The above command will update the line three. Consider the below output:
We can also use a regular expression to update more than one lines having the same pattern. Consider the below command:
The above command will update all the lines having string 'Apple is'. Consider the below output:
Transformation of Characters
The 'y' flag is used to transform the characters. The transformation of characters cannot be limited to specific occurrences. To transform characters, execute the command as follows:
The above command will transform the characters 'a', 'b', 'c' into 'd', 'e', 'f'. consider the below output:
Printing the Line Numbers
The '=' sign is used to print the line number. To print the line number, execute the command as follows:
The above command will display the line number of file content. Consider the below output:
The equal sign with the '-n' option specifies the line number that contains a matching script. Consider the below output:
The above command will display the line number that contains the word 'mango'. Consider the below output:
From the above output, we can see the line number 2 has the 'mango' word.