Linux sed Command Options - Explained with examples ?

SED stands for stream editor and it is a command in Linux that is used to perform different operations on files such as text replacement, insertion, text search, and etc. This command is extremely easy to use and can do wonders if used properly. 

Here at LinuxAPT, we shall look into a few features of the SED command followed by a few relevant examples so that you will be able to learn its usage.


Key Features of the SED Command includes:

  • Great for modifying the files without opening them which makes the overall process much quicker.
  • Allows you to make modifications by using different regular expressions with which you can conveniently search for different patterns.
  • Used for modifying strings within the terminal without using any file.
  • SED command is case-sensitive. Whichever word you wish to change or replace with this command, you will have to write it in the exact cases, otherwise, it will not work properly.


Examples of using the SED Command

To use the SED command in Linux, you will have to go through the following four examples. Before proceeding with these examples, we would like to share with you the text file that we will be using in all of these examples.

For displaying the contents of this file, we will use the "cat" command:

$ cat file.txt


1. Replace the First Occurrence of a Word in Every Line of the File

Here, we will be using the SED command for replacing the first occurrence of a word in every line of our sample text file. To achieve this, we have used the SED command:

$ sed 's/Hello/Hi/' file.txt

You can see that we have used the "s" operator for specifying the substitution operation that is going to take place. "Hello" is the word that we want to replace whereas "Hi" is the word that we want to replace it with. "file.txt' is the name of the file in which this operation will take place.

Once you run this command, its output will immediately appear on the terminal. You can conveniently verify that this command has successfully replaced the first occurrence of the specified word in all the lines of our target text file.


2.  Replace the nth Occurrence of a Word in Every Line of the File

Here, we will use the SED command for replacing the nth occurrence of a word in every line of our sample text file. Now, use the SED command:

$ sed 's/Hello/Hi/2' file.txt

With this command, we want to substitute the second occurrence of the word "Hello" with the word "Hi" in every line of our file. You can replace "2" with any other number depending on the position of occurrence of the specified word where you wish to make the substitution.

The output will show that the second occurrence of our specified word in every line of our file has been replaced successfully.


3. Replace all the Occurrences of a Word in the File

Now, we want to replace all the occurrences of a word in our sample text file at once. For that, we are going to use the SED command:

$ sed ‘s/Hello/Hi/g’ file.txt

Here, we have used the "g" flag of the SED command for making a global replacement of the specified word: all the occurrences of this word will be substituted at once.

The output of this command will confirm that all the occurrences of the specified word have been replaced successfully.


4. Replace the First Occurrence of a Word in a Specific Line of a File

Here, we will use the SED command for replacing the first occurrence of the specified word only within the specified line. For that, we will use the SED command:

$ sed ‘2 s/Hello/Hi/’ file.txt

With this command, we want to replace the first occurrence of the specified word only in the second line of our sample text file because of which we have used the number "2". You can replace it with any other number of your choice depending on the line number of the line where you wish to make the replacement.

You can witness from the output that the first occurrence of our specified word has been replaced successfully in the second line of our sample text file.


[Need help in fixing Linux system issues ? We can help you. ]

This article covers the concept of the SED command in Linux along with a few useful examples to elaborate on its usage. In fact, SED is a text stream editor used on Unix systems to edit files quickly and efficiently. The tool searches through, replaces, adds, and deletes lines in a text file without opening the file in a text editor.


What is the Linux sed Syntax ?

The main syntax for using the Linux sed command is:

$ sed OPTIONS... [SCRIPT] [INPUTFILE...]


What is sed Linux Options ?

You can execute sed with the following command-line options:

  • -b, --binary Open input files in binary mode to consider lines to end at a line feed.
  • --debug Switch to debug mode to print input in canonical form and annotate program execution.
  • --follow-symlinks Edit the ultimate destination if the specified file is a symbolic link. It only works when combined with the -i option.
  • --help Display the usage information.
  • --i, --in-place [=SUFFIX] Perform edits in-place by overwriting the original file.
  • --posix Disable all extensions to POSIX sed to simplify writing portable scripts.
  • --version Display the version of sed running on the system.
  • -E, -r, --regexp-extended Use extended regular expressions.
  • -e script, --expression=script Add a specified script to run with the commands.
  • -f script-file Add the contents of a specified script-file to run with the commands.
  • -l N, --line-length=N Define the desired line-wrap length for the l command (default value is 70).
  • -n, --quiet, --silent Disable output printing.
  • -s, --separate View specified files as separate, not as a single continuous long stream.
  • --sandbox Disable running external programs and operate only on input files on the command line.
  • -u, --unbuffered Minimalize input and output buffer.
  • -z, --null-data, --zero-terminated View input as a set of lines where each ends with a zero byte.

Related Posts