What is Sed Linux Command and How to Use it? 10
0

The sed command is a stream-based text editor that works on a replacement basis. It can be used to search, insert, replace and delete fragments in a file. With this utility you can edit files without opening them. It will be much faster if you write what and what needs to be replaced, than you will open the vi editor, search for the desired line and manually replace everything.

In this article, we will cover the basics of using the sed linux command, its syntax, and the regular expression syntax that is used directly to search and replace in files.

The content of the article:

 

 

Sed command on Linux

First, consider the command syntax:

$ sed options -e teams file

And here are its main options:

  • -n, –quiet – do not display the contents of the template buffer at the end of each iteration;
  • -e – commands to be executed for editing;
  • -f – read editing commands from a file;
  • -i – make a backup copy of the file before editing;
  • -l – indicate your string length;
  • -r – enable support for extended regular expression syntax;
  • -s – if several files are transferred, consider them as separate streams, and not as one long one.

I understand that now everything is very complicated, but by the end of the article everything will become clear.

1. How sed works

Now you need to understand how the sed command works. The utility has two buffers, it is an active template buffer and an additional buffer. Both are initially empty. The program fulfills the specified conditions for each line in the file passed to it.

sed reads one line, removes all trailing and newline characters from it, and places it in the template buffer. Then the commands transferred in the parameters are executed, an address can be associated with each command, this is a kind of condition and the command is executed only if the condition is suitable.

When all commands are executed and the -n option is not specified, the contents of the template buffer are output to standard output before the line feed character is added back. if it was deleted. Then a new iteration of the loop starts for the next line.

If no special commands are used, for example, D, then after the completion of one iteration of the loop, the contents of the template buffer are deleted. However, the contents of the previous line are stored in an additional buffer and can be used.

2. Sed addresses

Each command can be given an address that will point to the lines for which it will be executed:

  • number – allows you to specify the line number in which to execute the command;
  • first ~ step – the team will be executed for the time specified in the first part, and then for all with the specified step;
  • $ – the last line in the file;
  • /regular expression/ – any string that matches the regular expression. The l modifier indicates that the regular expression should be case insensitive;
  • number, number – starting from a line from the first part and ending with a line from the second part;
  • number, / regular_expression / – starting from the deadlines from the first part to the deadlines that will correspond to the regular expression;
  • number + quantity – starting from the line number indicated in the first part and another plus quantity lines after it;
  • number, ~ number – starting from line number to line number which will be a multiple the number.

If the command has not been given an address, then it will be executed for all lines. If a single address is passed, the command will be executed only for a line at this address. You can also pass a range of addresses. Then the addresses are separated by a comma and the command will be executed for all addresses in the range.

3. Regular expression syntax

You can use the same regular expressions as for Bash and popular programming languages. Here are the main operators that support regular Linux sed expressions:

  • * – any character, any quantity;
  • + – like an asterisk, only one character or more;
  • ? – no or one character;
  • {i } – any character in the amount of i;
  • {i, j } – any character in an amount from i to j;
  • {i, } – any character in an amount of i or more.

4. sed commands

If you want to use sed, you need to know the editing commands. Consider the most commonly used ones:

  • # – comment, not executed;
  • q – terminates the script;
  • d – removes the template buffer and starts the next iteration of the loop;
  • p – display the contents of the template buffer;
  • n – print the contents of the template buffer and read the next line into it;
  • s / what_replace / for_what_replace / options – replacement of characters, regular expressions are supported;
  • y /characters/characters – allows you to replace the characters from the first part with the corresponding characters from the second part;
  • w – write the contents of the template buffer to a file;
  • N – add line feed to the template buffer;
  • D – if the template buffer does not contain a new line, delete its contents and start a new iteration of the loop, otherwise delete the contents of the buffer before the line feed character and start a new iteration of the loop with what remains;
  • g – replace the contents of the template buffer with the contents of the additional buffer;
  • G – add a new line to the contents of the template buffer, then add the contents of the additional buffer there.

The utility can send several commands, for this they must be separated by a semicolon or use the two -e options. Now you know everything you need and you can proceed to the examples.

Sed examples

Now let’s look at sed Linux examples so that you have a holistic picture of this utility. First, let’s output from the construction file from fifth to tenth. To do this, use the -p command. We use the -n option not to display the contents of the template buffer at each iteration, but only what we need. If the command is one, then the option -e you can omit and write without it:

sed -n '5,10p' /etc/group

What is Sed Linux Command and How to Use it? 11

Or you can print the entire file, except lines one through twenty:

sed '1,20d' /etc/group

What is Sed Linux Command and How to Use it? 12

Here, on the contrary, we do not specify the -n option to display everything, but with the help of the d command we clear the unnecessary. Next, consider the replacement in sed. This is the most common function that is used with this utility. Replace the entries of the word root with losst in the same file and print everything to standard output:

sed 's/root/losst/g' /etc/group

What is Sed Linux Command and How to Use it? 13

The g flag replaces all occurrences; you can also use the i flag to make the sed regular expression case-insensitive. You can set addresses for teams. For example, let’s replace 0 with 1000, but only in lines one through ten:

sed '1,10 s/0/1000/g' /etc/group

What is Sed Linux Command and How to Use it? 14

We move even closer to the regular expression, delete all empty lines or lines with comments from the Apache config:

sed '/^#|^$| *#/d' /etc/apache2/apache2.conf

What is Sed Linux Command and How to Use it? 15

This regular expression (address) includes all lines that begin with #, are empty, or begin with a space, followed by a pound. Regular expressions can also be used when replacing. For example, replace all occurrences of p at the beginning of the line with losst_p:

sed 's/($p*)/losst_p/g' /etc/group

What is Sed Linux Command and How to Use it? 16

If you need to write the replacement result back to the file, you can use the standard output redirection operator> or the tee utility. For example:

sed '/^#|^$| *#/d' /etc/apache2/apache2.conf | sudo tee /etc/apache2/apache2.conf

You can also use the -i option, then the utility will not make changes to the file passed to it:

sudo sed -i '/^#|^$| *#/d' /etc/apache2/apache2.conf

What is Sed Linux Command and How to Use it? 17

If you want to save the original file, just pass the -i option in the extension parameter for the backup file.

findings

In this article, you learned what sed Linux command is. As you can see, this is a very flexible tool that allows you to do a lot with text. It is difficult to learn, but with it it is very convenient to solve many problems of editing configuration files or filtering output.

The Best Alternatives to Photoshop and Illustrator

Previous article

How to Install MySQL Server on Windows 10

Next article

You may also like

Comments

Leave a Reply

More in How To