HomeHOWTOSHow to use sed command in Linux

How to use sed command in Linux

Today we will be learning how to use sed command in Linux. In Linux, the SED command stands for Stream EDitor and is useful for a variety of frequently used actions in text files and streams. Sed assists with tasks such as choosing text, substituting text, changing an original file, adding lines to text, and deleting lines from the text.

Follow this tutorial to learn how to use sed command on your Linux system.

We produced a sample file called tutorfile.txt with the following content for this tutorial:

$ cat tutorfile.txt
Hey it's linux
Linux is a farmily of open-source Unix-like operating system the kernel
An operating system kernel first released on september 17,1991 
linux is the best operating system when it comes to security 
Hey let's switch to linux

To replace text with sed, use the substitution command s and delimiters (slashes – / in most situations) to separate text fields.

Syntax:

sed 's/old_string/new_string/' filename.txt

To replace instances of Linux with the word Unix, for example, type:

$ sed 's/linux/unix/' tutorfile.txt
Hey it's unix
Linux is a farmily of open-source Unix-like operating system the kernel
An operating system kernel first released on september 17,1991 
unix is the best operating system when it comes to security 
Hey let's switch to unix

If you have numerous instances of the same term on a single line, use the ‘g‘ flag to modify them all.

If you want to consistently replace the term Unix with the word better in the file tutorfile.txt, type:

$ sed 's/Unix/better/g' tutorfile.txt
Hey it's linux
Linux is a farmily of open-source better-like operating system the kernel
An operating system kernel first released on september 17,1991 
linux is the best operating system when it comes to security 
Hey let's switch to linux
Hey better the best let's switch to better

The sed command is case-sensitive by default. For instance, the word “linux” appears in the material below with both upper- and lowercase first letters.

To replace every occurrence of the word “linux” with “unix” run the command :

 $ sed 's/linux/unix/'  tutorfile.txt
Hey it's unix
Linux is a farmily of open-source Unix-like operating system the kernel
An operating system kernel first released on september 17,1991 
unix is the best operating system when it comes to security 
Hey let's switch to unix
Hey Unix the best let's switch to Unix

Add the” i “subcommand at the end of the command to ignore case when substituting text.

syntax”

sed 's/old_string/new_string/i' filename.txt

As a result, the command for altering upper and lowercase instances of the word linux in the preceding text is:

$ sed 's/linux/unix/i' tutorfile.txt
Hey it's unix
unix is a farmily of open-source Unix-like operating system the kernel
An operating system kernel first released on september 17,1991 
unix is the best operating system when it comes to security 
Hey let's switch to unix
Hey Unix the best let's switch to Unix

You can use the sed command to replace a string in a specific line by prefixing the “s” subcommand with the line number.

syntax:

sed '# s/old_string/new_string/' filename.txt

To test this functionality on the sample tutorfile.txt file, replace the word security with configure only in the fourth line (4) of the text using the command:

 $ sed '4 s/security/configure/' tutorfile.txt
Hey it's linux
Linux is a farmily of open-source Unix-like operating system the kernel
An operating system kernel first released on september 17,1991 
linux is the best operating system when it comes to configure 
Hey let's switch to linux
Hey Unix the best let's switch to Unix

To replace numerous instances of a string inside a line range, rather than the complete text, provide the range where sed should substitute.
The syntax is as follows:

sed '#,# s/old_string/new_string/' filename.txt

Replace the first # with the first line number you want to include, and the second # with the last line number you want to include.

I.e to replace hey with the user in line 1 and 5.

$ sed '1,5 s/Hey/user/' tutorfile.txt
user it's linux
Linux is a farmily of open-source Unix-like operating system the kernel
An operating system kernel first released on september 17,1991 
linux is the best operating system when it comes to security 
user let's switch to linux
Hey Unix the best let's switch to Unix'

With the sed command, use the “d” subcommand and the following syntax to remove a line from a file:

sed '#d' filename.txt

Specify the line number you want to remove instead of the hash (#) symbol and run the command.

I.e to remove line 6;

$ sed '6d' tutorfile.txt
Hey it's linux
Linux is a farmily of open-source Unix-like operating system the kernel
An operating system kernel first released on september 17,1991 
linux is the best operating system when it comes to security 
Hey let's switch to linux
-

To delete multiple lines within a line range using the sed command with “d “option, follow the syntax:

sed '#,#d' filename.txt

Replace the hash symbols with the beginning and end of the line range.

I.e to remove lines 2 to 4.

 $ sed '2,4d' tutorfile.txt                                                
Hey it's linux


Hey let's switch to linux
Hey Unix the best let's switch to Unix

To delete lines from a specific line number to the last line.

Syntax:

sed '#,$d' filename.txt

Delete everything from line 3 to the end of tutorfile.txt, in other words.

$ sed '3,$d' tutorfile.txt
Hey it's linux
Linux is a farmily of open-source Unix-like operating systemon the kernel
-
-

Peroration

You learnt how to use the text stream editor SED to alter a file from the command line in this tutorial.
After you’ve made your changes to the text and saved the most recent version, you can decide whether or not to keep the original file.

Other tutorials:

Install KDE Desktop Environment on Ubuntu 22.04|20.04

How To Install TeamViewer on Ubuntu 22.04|20.04

How To Install Google Chrome on Ubuntu 22.04|20.04

- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here