Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

Top 20 sed commands to use

Hello, friends. The sed command is an overused command by sysadmins around the world. It is basically used to replace text by text in a text file with a single command. That’s why, today, we are going to present you the Top 20 sed commands. For this, we will look at the most useful and common ones.

Top 20 sed commands to use

Replace a string with another string

The sed command is used to replace strings. But it is so flexible that we can customize the execution to suit our needs.

In a text file to change the string macOS to Linux we would have to run

sed 's/macOS/Linux/' [file]

Then, you can adapt the command by the strings you want.

2.- Replace a certain number of occurrences of a string

- Advertisement -

The above command replaces the string only once. You can specify the order in which the string is replaced.

sed 's/macOS/Linux/2' [file]

In this case, it will replace only the second occurrence.

3.- Replace all occurrences of a string

sed 's/macOS/Linux/g' [file]

4.- Replace a string on a specific line number.

Another useful option is to replace a string of characters on a line number. This is useful if you know the file well.

- Advertisement -
sed '[line] s/macOS/Linux/' [file]

For example, to replace a string on the third line.

sed '3 s/macOS/Linux/' [file]

5.- Replacing characters within a range

To replace characters but within a specified range of lines in a file.

sed '30,40 s/macOS/Linux/g' [file]

In this case, the string will be replaced only between lines 30 and 40.

6.- Insert line breaks in files

It is also possible to insert a blank line break. To achieve this, you have to execute.

sed G [file]

Very useful in files where everything is close together.

7.- Delete lines from a file

Although the sed command is used to replace strings, you can actually do many things with it. One of them is to delete a line. To do this run.

sed 'nd' [file]

Replace n with the number of the line to delete.

8.- Show the whole file except for a certain range of lines.

You can also display a file minus a range of lines.

sed '20,35d' [file]

In this case, the entire file will be shown except from line 20 to 35.

9.-View a specific range of lines in a document

If you want to display a specific range within the file, You have to execute the following command:

sed -n 'Ni,Nlp' [file]

Where you have to replace Ni with the start line number and Nl with the end.

sed -n '5,15p' [file]

In this case, it will show the range of lines 5 to 15.

10.- Delete matching lines in a text file

You can also delete lines that match a specific string of text.

sed '/macOS/d' [file]

In this case, all lines with the word macOS will be deleted.

11.- Print the line number in the file

To print the line number in a text file with the command sed. To achieve this, we only have to execute the following command.

sed '=' [file]

This way when the file is printed, the line number will be displayed.

12.- Delete all lines of a file except the specified ones.

Now you can delete all lines in a file except those you specify.

sed '15,21!d' [file]

This way the file will delete all lines except those from 15 to 21.

13.- Replace all uppercase characters with lowercase characters using sed

The case of uppercase and lowercase is always relevant when dealing with text files. So with sed you can replace all uppercase characters with lowercase characters.

sed 's/\(.*\)/\L\1/' [file]

14.- Delete any line that begins with a string

If you want to delete all lines that start with a specific string, you can run something similar to this

sed '/^macOS/d' [file] 

15.- Replace all the occurrences of a string from one line to the end

In this case, you can replace one string with another from a certain line to the end of the string

sed '3,$s/macOS/Linux/g' [file]

In this case, I have chosen line 3, but it can be any line.

16.- Print a series of lines from a file

If you want to print just a series of lines you can also do it with sed as follows

sed -n '2,10p' [file]

In this example, all lines from 2 to 10 will be printed.

Delete blank lines after each line in a file

Deleting blank lines can be very useful in configuration files or other files where we need to process it.

To delete these blank lines you can run

sed '/^$/d'[file]

It’s as simple as that.

Capitalize the first character of each word

This is one of the most useful and most used options. You can replace the first letter of each word by its equivalent in capital letters.

To achieve this, just execute this command

sed 's/\([a-z]\)\([a-zA-Z0-9]*\)/\u\1\2/g'

It is effortless.

Remove the last line of a file

There are changes we need to make in a configuration file, and we usually place it in the last line. What if we want to delete it? Well, you can remove the last line of a file by running

sed '$d' [file]

20.- Show the help of the sed command

Finally, you can consult the help for the command execute

sed --help

And you will get an output screen like this.

sed command help
sed command help

Thanks for reading and I hope you liked it and that it will be useful for your work.

sed command page

More terminal commands

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook