The sed command Linux

The command sed is a stream editor and works on the principle of replacement. It can be used for lookup, insertion, replacement and removal of fragments per file. With this utility you can edit the files without opening them. It will be much faster if you write what and what should be replaced than you will open the vi editor to find the line and manually replace everything.

In this article, we review the basics of the sed command linux, the syntax, and the syntax of regular expressions used for search and replace in files.

The sed command in Linux

First, consider the syntax of the command:

$ sed-e option file

But her main options:

  • -n, –quiet – do not output contents of the stencil buffer at the end of each iteration;
  • -e commands you need to run to edit;
  • -f – read editing commands from a file;
  • -i – to make a backup of the file before editing;
  • -l – specify the length of the string;
  • -r to enable extended regular expression syntax;
  • -s – if given multiple files, consider them as separate threads, rather than as one long one.

I know things are very difficult, but the end of the article all will become clear.

1. How does sed

Now you need to understand how the sed command. The utility has two buffers is the active buffer pattern and the additional buffer. Both initially empty. The program performs the specified conditions for each line to the passed file.

sed reads one line, deletes all trailing characters and the newline and places it into the buffer template. Then executed the passed in parameters to the command with each command may have an associated address is a kind of condition and the command is executed only if a condition.

When all commands will be executed and not provided the-n option, contents of the stencil buffer is written to standard output before it is added back the newline character. if it was deleted. Then starts a new loop iteration for the next row.

If you do not use special commands, e.g. D, then after one iteration of the loop the contents of the buffer pattern is removed. However, the content of the previous line is stored in the extra buffer and you can use it.

2. Addresses sed

Each team can pass the address, which will point to the rows for which it will be executed:

  • room – allows you to specify the line number at which to execute the command;
  • first~step – the command will be performed for the specified first part time and then for all the specified step;
  • $ – the last line in the file.
  • /regexp/ – any string that matches that regular expression. The modifier l specifies that the regular expression should be case-insensitive;
  • the room, the room – from the rows of the first part and ending with the string from the second part;
  • number, /regexp/ – from the time of the first part and to the terms that will match a regular expression.
  • number +number – starting line number specified in the first part plus the number of lines after it;
  • number ~number starting from line number and to line number which is a multiple of the number.

If the command was not addressed, it will be done for all rows. If the passed-in address, the command will only be executed for the row at this location. You can also pass a range of addresses. Then the addresses are separated by a comma and the command is executed for all addresses in the range.

3. Regular expression syntax

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

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

4. Command sed

If you want to use sed, you need to know editing commands. Consider the most frequently used of them:

  • # – a comment is not executed.
  • q – quits the script.
  • d – delete the pattern buffer and starts the next loop iteration;
  • p – print the contents of the stencil buffer;
  • n – output the contents of the stencil buffer and read the following line;
  • s/tstat/natstat/options – character replacement, supports regular expressions;
  • y/symbols/symbols – allows you to replace characters from the first part of the corresponding characters of the second part;
  • w – write the contents of the stencil buffer to a file;
  • N – add a newline to the buffer pattern;
  • D – if the buffer of the template does not contain a new line, delete its contents and start a new iteration of the loop, otherwise remove the contents of the buffer up to the newline character and start a new iteration cycle with the rest;
  • g – replace the contents of the stencil buffer, the contents of the additional buffer;
  • G – add a new line to the contents of the stencil buffer, then add additional content to the same buffer.

Utility you can send several commands, for that they should be separated by a semicolon or to use two of the-e option. Now you know everything you need and you can skip to the examples.

Examples of using sed

Now consider the examples sed Linux, you to have a complete picture about this utility. Let us first derive from a file construction five through ten. For this we use the command -p. We use the-n option to not output the contents of the stencil buffer at each iteration, and print only what we need. If the command is one, then the option -e to omit and write without it:

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

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

sed '1,20 d' /etc/group

Here, on the contrary, the-n option is not specified to output all and use the command clean d unnecessary. Next, consider the replacement in sed. It is the most common function that is used with this utility. Replace instances of the word root on losst in the same file and output everything to standard output:

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

The g flag replaces all occurrences, you can also use the i flag to make the sed regular expression not case sensitive. For commands you can specify addresses. For example, let us replace 0 to 1000, but only rows from the first to the tenth:

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

Go even closer to the regular expression, remove all empty lines or system review from your Apache configuration:

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

Under this regular expression (address) covered all the lines that start with #, empty, or starts with a space, and followed by a grating. Regular expressions can be used in replacement. For example, replace all occurrences of p at the beginning of the line on losst_p:

sed 's/[$p*]/losst_p/g' /etc/group

If you need to record the result of replacing in back into the file, you can use standard operator output redirection > or utility tee. 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 passed file:

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

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

Insights

From this article you learned what is the sed command Linux. As you can see, this is a very flexible tool that allows you to do text a lot. It is difficult to learn, but using it is very convenient to solve many tasks by editing configuration files or output filters.

Source: losst.ru

(Visited 16 times, 1 visits today)