10 ways to use the sed command on Linux

The sed command on Linux can be used in quite a few different ways. This “stream editor” (as the name implies) provides options to help you manipulate text very efficiently. Once you become familiar with the sed command syntax, you’ll likely come to appreciate its functionality and use it quite often.

1. Easy find/replace option

The most common use of sed is to replace a word or phrase in a line of text. For example, to change “I want” to “I do not want” in a string, you could use a command like the one shown below that demonstrates how it would work. Note the sed command changes “want” to “do not want”.

$ echo "I want to join the meeting" | sed 's/want/do not want/'
I do not want to join the meeting

Of course, commands like this make more sense when you’re working with text in a file. Keep in mind, however, that a command like that shown below changes the output displayed, but not the content of the file – in this case, the plans file.

$ sed 's/want/do not want/' plans
I do not want to join the meeting today.
I will be off the next two weeks.

To modify the file, not just the command output, you need to use the -i option like this:

$ sed -i 's/want/do not want/' plans

Then, when you look at the file, you’ll notice that it’s been updated.

$ cat plans
I do not want to join the meeting today.
I will be off the next two weeks.

The -i options means “edit files in place”.

Keep in mind that it’s a good idea to test your changes to files before you make them permanent, especially if you’re just getting used to the command. You can omit the “-i” and, if the output looks like what you expected, run the same command with the -I to proceed with the changes to the file.

2. Global replacements

To replace every instance of a certain word in a file, you would need to use a command like the one shown below where the “g” means “global”. The command will change every instance of “Mar” to “Apr” in the schedule file. Again, because it includes the -i (lowercase “i”) option, the file is changed in place.

$ sed -i 's/Mar/Apr/g' schedule

If Mar might be spelled as “Mar” or “MAR”, you can add the case-insensitive option (g) in a command like this:

$ sed -i 's/Mar/Apr/Ig' schedule

Note that it’s the “I” (capital i) before the “g” that makes the command case-insensitive.

3. Specific line changes

To change only one specific line in a file, use a command like the one below that includes the line number (i.e., 2) to ensure that the change is only made in the specified line.

$ sed -i '2s/Mar/Apr/' schedule

4. Deleting lines

You can also use sed to delete lines in a text file. The command below would delete line 3 in the file.

$ sed -i '3d' schedule

The command below would delete the lines 2-4.

$ sed -i '2,4d' schedule

5. Displaying matching lines’

To display only those lines in a file that contain specific text, use a command like this:

$ sed -n '/bills/p' schedule
Apr 1: pay bills

6. Inserting lines

To insert a line before a line that contains specific text, use a command like that shown below that inserts the line “IMPORTANT” before the line containing the word “bills”.

$ sed -i '/bills/i IMPORTANT' schedule
$ cat schedule
IMPORTANT
Apr 1: pay bills
Apr 8: check accounts
Apr 22: take a break

To insert a line following specific content, use a command like this:

$ sed -i '/bills/a REMAINING' schedule
$ cat schedule
IMPORTANT
Apr 1: pay bills
REMAINING
Apr 8: check accounts
Apr 22: take a break

7. Replacing multiple words

To replace multiple words in your sed output, you can use a command like that shown below.

$ sed -e 's/IMPORTANT/DONE/g' -e 's/REMAINING/CONTINUE/g' schedule
DONE <= replaced
Apr 1: pay bills
CONTINUE <= replaced
Apr 8: check accounts
Apr 22: take a break

In this case, the file is not updated. Save the output if you want it to be retained.

8. Removing blank lines

To remove blank lines in a file, use a command like this:

$ sed -i '/^$/d' schedule

9. Extracting content

To extract a series of lines from a file, use a command like this that displays lines 3-5:

$ sed -n '3,5p' schedule
REMAINING
Apr 8: check accounts
Apr 22: take a break

You can send the output to another file with a command like that shown below if you need to preserve it as a separate file.

$ sed -n '3,5p' schedule > save

10. Removing leading and trailing blanks

To remove leading and trailing blanks from a file, use a command like that shown below. First, here’s the file before the change:

$ cat errands
March errands
feed the cats every day
take trash to recycling center
write a poem

Here’s the command that strips the leading and trailing blanks and the updates the file.

$ sed -i 's/^[ t]*//;s/[ t]*$//' errands
$ cat errands
March errands
feed the cats every day
take trash to recycling center
write a poem

Wrap-up

The sed command offers a lot of options for selecting content from a command or a file and changing it as requested. Play with the options and get used to them before you start depending on them for your daily work.

Source:: Network World