Sed command in Linux

The sed command, short for "stream editor," is a powerful text processing tool used in Unix and Unix-like operating systems. It allows users to perform basic text transformations on an input stream (such as a file or input from another command). Common operations include searching, find-and-replace, and text substitution.

The SED (Stream Editor) is a simple yet powerful utility that parses text and transforms it seamlessly. It was developed by Lee E. McMahon of Bell Labs in 1973 1973–1974.

Basic uses of sed command.

The sed command can be used to:

  1. Search and replace text

  2. Delete lines containing a pattern

  3. Insert text before a line

  4. Print specific lines

  5. Replace text globally in a file.

Working with the sed command

To work with the sed command, you must have it installed on your terminal or working environment.

To install sed, use this command

sudo apt-get install sed

To check the version of sed installed, use this command

sed --version

That command will show you the version of sed that is installed on your terminal or working environment.

Now that you have sed installed, lets now get working.

On your terminal create a file name artist.txt with this content,

1. DAMN., Kendrick Lamar, April 14, 2017
2. 25, Adele, November 20, 2015
3. To Pimp a Butterfly, Kendrick Lamar, March 15, 2015
4. Lemonade, Beyoncé, April 23, 2016
5. ÷ (Divide), Ed Sheeran, March 3, 2017

Let's now use sed to edit the contents of this file.

  1. Printing the content of the file

    To print the content of the file, we will use this command.

     sed 'p' artist.txt
    

You would notice that the content of the file is printed out twice, to suppress this we will use the '-n' option to suppress printing of the buffer.

sed -n 'p' artist.txt

We can also print specific lines; we can specify the address of the line we want to print.

sed -n '2p' artist.txt

The command will print the second line of our artist.txt file.

We can also print from one address to another using the comma (,).

sed -n '2,4p' artist.txt

This will print from address 2 to 4.

Special Character Dollar ($) which represents the last line of the file to print it. Using the $ sign we can use the last line.

sed -n '$p' artist.txt

This will print the 5th address, which is the last line of the file.

We can also use the plus (+) to print from one range to another.

sed -n '1+3p' artist.txt

This will print from address 1 to address 4.

We can also use the tilde (~) to print from one range to another.

sed -n '1~2p' artist.txt

This will print address 1, 3, 5. Basically, once it prints its first address it moves two more lines to print the next address.

You can also specify a pattern range to print the address.

sed -n '/Kendrick/ p' artist.txt

This command will match the pattern and print each line containing 'kendrick'.

You can also specify the range of address that should be printed using a pattern.

sed -n '/Kendrick/, 5p' artist.txt

Basic Commands of sed

  1. Delete command

    The sed command can be used to delete address using a pattern or specifying range of addresses to be deleted.

     sed 'd' artist.txt
    

    Since no line address is provided, sed deletes all the lines from the pattern buffer.

     sed '3d' artist.txt
    

    This will delete the fourth line. We can also specify range of address or pattern to delete.

  2. Append Command

    This will append a text to a specified address or pattern.

     sed '5 a 6. Future Nostalgia, Dua Lipa, March 27, 2020' artist.txt
    

    This command will append a new artist after line number 5.

  3. Change Command

    This command is used to change what is an address.

     sed '4 c 4. After Hours, The Weeknd, March 20, 2020' artist.txt
    

    This command will change what is in line 4 to the string provided.

  4. Insert command

    The insert and append works in the same way, the only difference is that it inserts a line before the specific position.

     sed '3 i 7.  After Hours, The Weeknd, March 20, 2020' artist.txt
    

    This command will put the new string after line 2 and print the rest of the string.

There are other many uses of the sed command, but these are just the basic ones you need to know to start working with sed.

Do well to like and comment any part you do not understand and need clarification on.


References:

  1. [https://www.geeksforgeeks.org/sed-command-in-linux-unix-with-examples/]

  2. [https://phoenixnap.com/kb/linux-sed]