Javatpoint Logo
Javatpoint Logo

Linux sed Command | Linux Stream Editor

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.

Syntax:

Options:

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:

Linux Sed Filter

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.'

Global Replacement

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.

Syntax:

Consider the below examples:

The above commands will replace all the specified text pattern. Consider the below output:

Linux Sed Filter

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:

Linux Sed Filter

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:

Linux Sed Filter

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:

Linux Sed Filter

From the above output, we have used commands applied to the earlier example. So, the output is the same as the previous example.

Replacing Characters

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:

  • A range of lines.
  • A pattern that matches a specific line.

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:

Linux Sed Filter

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:

Linux Sed Filter

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:

Linux Sed Filter

To append text, execute the command as follows:

The above command will append the text. Consider the below output:

Linux Sed Filter

Modifying Lines

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:

Linux Sed Filter

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:

Linux Sed Filter

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:

Linux Sed Filter

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:

Linux Sed Filter

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:

Linux Sed Filter

From the above output, we can see the line number 2 has the 'mango' word.


Next TopicLinux tee




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA