How to use the Sed command in Linux

How to use the Sed command in Linux – Introduction

Searching, find-and-replace, insertion, and deletion are just a few of the many file-related functions available through the UNIX SED command, which stands for stream editor. Although the SED command is most frequently used for substitution or search and replace.

SED allows you to modify files even without opening them, which makes finding and replacing content in a file faster than doing so by first opening the file in the VI Editor.

Syntax

The Sed command syntax consists of three components.

  • The Linux command’s output is governed by options.
  • A list of Linux commands to run is included in the script.
  • The file on which you are running the sed command is represented by the file name (with extension).

A sed command can be executed without any arguments. The script can also be launched without a filename, in which case it uses the standard input data.

The usage of the Sed command is given below.

  1. Basic text substitution

With the sed tool, any specific part of a text may be searched for and changed using a pattern. The task of search and replace is denoted by the letter’ in the example below. The text “Bash Scripting Language” will be searched for the term “Bash,” and if it is found, the word “Perl” will be substituted.

echo “Bash Scripting Language” | sed ‘s/Bash/Perl/’

The term “Bash” appears in the output of this operation. As a result, “Perl Scripting Language” is the output.

You may substitute any portion of a file’s content with the sed program. Create a text document called weekday.txt and add the following data to it.

 

$ cat weekday.txt
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

The command below will look for the word “Sunday” and replace it with the phrase “Sunday is a holiday.

sed ‘s/Sunday/Sunday is holiday/’ weekday.txt

After running the aforementioned sed command, the term “Sunday” that was present in the weekday.txt file is changed with the text “Sunday is a holiday” as the result of this execution.

  1. Replace all instances

To learn how to utilize the ‘g’ option in the sed program, create a text file titled python.txt that contains the following text and the term “Python” many times.

$ cat python.txt
Python is a very popular language.
Python is easy to use. Python is easy to learn.
Python is a cross-platform language

 

The second line of the file python.txt’s second line will be changed whenever the word “Python” appears with the following command. Python appears twice in the second line of this sentence.

sed ‘2 s/Python/perl/g’ python.txt

The script’s output will look like this once it has finished running. Here, “Perl” is used in place of every instance of “Python” in the second line.

2.	Replace all instances

  1. On each line, replace just the second instance of a match

If a word occurs more than once in a file, the specific instance of the term on each line can be substituted using the sed command and the number of occurrences. The second occurrence of the search pattern in every line of the file python.txt will be changed by the following sed command.

sed ‘s/Python/perl/g2’ python.txt

After executing the aforementioned command, the output listed below will show up. In this case, the text “Python” is only used twice in the second line before being substituted by the word “Perl”.

replace just the second instance of a match

  1. On each line, replace only the last instance of a match

‘lang.txt’ should be a text file with the following information in it.

$ cat lang.txt
Bash Programming Language.  Python Programming Language. Perl Programming Language.
Hypertext Markup Language.
Extensible Markup Language.

 

Use the sed command as follows to just replace the final occurrence of “Programming” with “Scripting” on a line.

sed ‘s/\(.*\)Programming/\1Scripting/’ lang.txt

Note that the pattern match in this sed command, (.*)Programming, will generate a capture group with all the text matching the line in a greedy manner up to the final instance of programming to comprehend this command. This capture group is addressed in the replace section of sed as “1Scripting” and is saved as “1”. As a result, the replacement line will contain the complete line match up to and including the final instance of the word “Programming,” with the phrase “Scripting” serving as the line’s terminus. This is the method to use to get the result that is displayed.

  1. Put new text after the 1st match in a file

The next command will just replace the string “perl” with the first match of the search pattern, “Python.” In this case, “1” is utilized to match the pattern’s very first appearance.

sed ‘1 s/Python/perl/’ python.txt

The output shown after executing the aforementioned instructions is as follows. Here. ‘perl’ is substituted for the word ‘Python’ in the first line.

Sed command in Linux

  1. Insert new text after the last match in a file

The next command will substitute the word “Bash” for the final instance of the search term “Python.” The ‘$’ sign is used in this case to match the pattern’s most recent occurrence.

sed -e ‘$s/Python/Bash/’ python.txt

The output shown after executing the aforementioned commands is as follows.

Sed command in Linux

  1. Managing search and replacement of file paths by escaping backslash in replace commands

For finding and replacing, the backslash in the file path must be removed. The file path will have a backslash (/) added with the sed command that comes next.

echo /home/ubuntu/code/perl/add.pl | sed ‘s;/;\\/;g’

When the sed program is used with the file path ‘/home/ubuntu/code/perl/add.pl’ as input, the following output will be produced.

Sed command in Linux

  1. All file paths should be replaced with simply the filename, no directory

The basename command makes it extremely simple to obtain the filename from the file path. The filename can also be retrieved from the file path using the sed program. Only the filename from the file path supplied by the echo command will be returned by the next command.

echo “/home/ubuntu/temp/myfile.txt” | sed ‘s/.*\///’

The output shown after performing the aforementioned command is as follows. ‘Myfile.txt’ is written as output in this case.

Sed command in Linux

  1. Replace text, but only if another piece of text is included in the string

To replace any text that is based on other text, make a file called “dept.txt” and fill it with the following information.

The following sed command uses two replace instructions. Here, the text “Count” will be changed to “100” in the line where the text “CSE” appears, and the text “Count” will be changed to “70” in the line where the search pattern “EEE” appears.

sed  -e ‘/CSE/ s/Count/100/; /EEE/ s/Count/70/;’ dept.txt

When the aforementioned command is executed, the output listed below will show up.

Sed command in Linux

  1. Replace text, but only if it cannot be found elsewhere in the string

The ‘Count’ value on the line that does not include the text ‘CSE’ will be changed by the following sed command. Two lines in the dept.txt file do not contain the word “CSE.” Thus, 80 will be added on two lines to replace the string “Count.”

sed -i -e ‘/CSE/! s/Count/80/;’ dept.txt

The output shown after executing the aforementioned commands is as follows.

Sed command in Linux

  1. Using ‘1’, add string both before and after the matching pattern

The numbers “1,” “2,” and so on are used to signify the order in which the “sed” command matches patterns. The following sed program will look for the pattern “Bash,” and if it does, the access key “1” will be used in the replacement text section. In this case, the text “Bash” is searched for in the input text and two texts are appended before and after “1”.

echo “Bash language” | sed  ‘s/\(Bash\)/Learn \1 programming/’

The output shown after performing the aforementioned command is as follows. Here, the words “Learn” and “programming” are placed before and after “Bash,” respectively.

Sed command in Linux

  1. Delete matching lines

The sed command’s “d” option can be used to remove any line from a file. To test the ‘d’ option’s functionality, create a file called ‘os.txt’ and add the following text to it.

The lines in the file “os.txt” that include the word “OS” will be removed by the sed command that follows:

sed ‘/OS/d’ os.txt

The output shown after executing the aforementioned commands is as follows:

Sed command in Linux

  1. Delete the corresponding line and the two lines that follow it

If the pattern ‘Linux’ is discovered, the following program will remove three lines from the file os.txt. ‘Linux’ appears on the second line of the file os.txt. This line as well as the next two lines will be removed.

sed ‘/Linux/,+2d’ os.txt

Sed command in Linux

Output

  1. Add anything to the end of the queue if there is a match in the queue

Make sure the following text is in the file “os.txt”:

Sed command in Linux

The ‘os.txt’ file’s line containing the word ‘Windows’ will have ’10’ appended to the end of it if the following command is used.

sed ‘/Windows/ s/$/ 10/’ os.txt

Following the execution of the command, the output will look like this.

Sed command in Linux

  1. Insert a line before the text if there is a match in the line

An ‘input.txt’ file must be present for this example.

Sed command in Linux

The ‘input.txt’ file will be searched for the phrase ‘PHP is platform-independent’ using the sed command that is displayed below. ‘PHP is an interpreted language’ will be added before that line if the text is present in the file.

sed ‘/PHP is platform-independent/ s/^/PHP is an interpreted language.\n/’ input.txt

Output

Sed command in Linux

  1. In place of a string from one list, use a new string from another list

To test this example, you must generate two list files. Add the following material to two text files, ‘list1.txt’ and ‘list2’, and then create them.

Sed command in Linux

By dividing the process into manageable phases, we may transform it from complex to simple. Our strategy is to first generate a file containing the list of the pertinent sed syntax replacement instructions using the substitution operator since sed may accept a list of substitution commands as a file input. Here is a straightforward piece of code that creates a command file using the low-complexity Linux program awk. Please take note that this is a cat of the original file, piped into awk, with the output sent to ‘command.txt’

cat list1.txt | awk -F: ‘{printf(“s/%s/%s/\n”, $1, $2)}’ > command.txt

With the ‘command.txt’ file ready, we can easily use the simple sed command to execute this list of substitutions on ‘list2.txt’:

sed -f command.txt list2.txt

The technique to change the matched keys and values is illustrated in the output below in two steps:

Sed command in Linux

  1. To divide the content into numerous lines, remove the commas and add a new line

The comma-separated line from the echo command will be passed into the next sed command, which will replace all commas with new lines.

echo “Kaniz Fatema,30th,batch” | sed “s/,/\n/g”

The output shown after performing the aforementioned command is as follows. Three comma-separated data are fed into the text, replaced with a newline, and printed across three lines:

Sed command in Linux

  1. Change the Case of Characters

You may use the ‘y/old/new/’ command to alter a string’s case. For instance, you may use the command below to convert all lowercase characters in the fruits.txt file to uppercase:

sed ‘y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/’ fruits.txt

 

  1. Print the range of file

The’start,endp’ commands can be used to print a range of lines. For instance, you can use the following command to print the first 10 lines of the file fruits.txt:

sed -n ‘1,10p’ fruits.txt

 

  1. Print line numbers only

‘/pattern/=’ can be used to output just the line numbers of lines that match. For instance, the following command may be used to output the line numbers of all lines in the file fruits.txt that have the word “apple” in them:

sed ‘/apple/=’ fruits.txt

 

Wrapping Up

For basic text modifications on an input stream, the text stream editor sed is included with Unix systems and command line shells. Without having to open the file, you can choose, swap out, duplicate, add, remove, and edit text.

It functions by making one pass over the inputs as a part of the Linux commands repository, making it a more effective utility than most. The program differs from other command line editors in that it also can filter text within a pipeline. Additionally, it works with regular expression, enabling complex text matching.

We trust that this article has provided you with further knowledge regarding the sed command. Please post your thoughts or questions in the space provided below.

Don’t forget to comment which linux commands you liked the most.

You can learn about linux more deeply by clicking the link below

https://linuxiron.com/what-is-linux-a-whole-introduction/

Learn about the linux commands by clicking the links below

https://linuxiron.com/echo-command-in-linux/

https://linuxiron.com/how-to-use-nice-renice-commands-in-linux/

https://linuxiron.com/how-to-use-kill-commands-in-linux/

https://linuxiron.com/a-beginners-guide-to-htop-for-process-management/

https://linuxiron.com/15-useful-yum-commands-in-linux/

https://linuxiron.com/how-to-use-the-top-command-in-linux/

https://linuxiron.com/17-ps-command-to-monitor-linux-process-with-examples-linuxiron/

https://linuxiron.com/12-cat-commands-in-linux-with-examples/

https://linuxiron.com/archiving-and-compressing-files-and-directories-in-linux/

https://linuxiron.com/how-to-run-the-du-command-in-linux/

Leave a Comment

Your email address will not be published. Required fields are marked *