Sed regular expressions. Advanced sed: managing text streams in Linux. Reading commands from a file

Document created: 02/16/2010

Another example

For i in * .txt; do sed -i "/ ^ \\ ^ / s / ^ \\ ^ \\ ([^ \\ ^] * \\) \\ ^ \\ ^ / \u003d\u003d\u003d\u003d \\ 1 \u003d\u003d\u003d\u003d /; / ^ \u003d / s / $ / \\ n ^ Command ^ Description ^ / "$ i; done

This line loops through all files in the current directory whose names end in .txt, and:

    / ^ \\ ^ / s / ^ \\ ^ \\ ([^ \\ ^] * \\) \\ ^ \\ ^ / \u003d\u003d\u003d\u003d \\ 1 \u003d\u003d\u003d\u003d / - if the line starts with "^", replace "^ Text ^ ^ ". to "\u003d\u003d\u003d\u003d Text \u003d\u003d\u003d\u003d";

    / ^ \u003d / s / $ / \\ n ^ Command ^ Description ^ / - if the line begins with "\u003d" (and it begins, because in the previous paragraph we did it ourselves, and here the condition is only for touch the rest of the lines), replace this line with it, plus a carriage return, plus "^ Command ^ Description ^". In other words, insert after such a line a line containing "^ Command ^ Description ^".

What is this for? That's what it is for. I have split a long dokuwiki file with linux commands into small files. They turned out to be like:

^ File System Analysis ^^ | badblocks -v / dev / hda1 | check hda1 partition for bad blocks | ...

This sequence of commands turned them all into view:

File System Analysis \u003d\u003d\u003d\u003d ^ Command ^ Description ^ | badblocks -v / dev / hda1 | check hda1 partition for bad blocks | ...

Deleting a row by context

To delete a line containing a specific context, you can use the following construction:

Sed -i "/ ^ AUTO_SAVE / d" notes.ini

This command in the notes.ini file removes all lines starting with AUTO_SAVE.

Deleting a line or multiple lines in a file

To delete a line or multiple lines in a file, I use the following construction:

Sed -i "2,1d"

This command in the file deletes the second line.

Sed -i "5,10d"

This command in the file will delete ten lines starting from the fifth (including the fifth).

Concatenating strings

Concatenate two adjacent strings in pairs

Concatenate two lines in pairs separated by a carriage return:

Cat / etc / hosts | sed "N; s / \\ n / - /"

PS. Separator: " - ".

Converting to upper or lower case

To uppercase:

Echo Sed | sed "s /.*/ \\ U & /" SED

To lower case:

Echo Sed | sed "s /.*/ \\ L & /" sed

"Cut" a piece of the stream

Let's take the output of dig as an example:

$ dig ya.ru

will give us

; <<>\u003e DiG 9.7.0-P1<<>\u003e ya.ru ;; global options: + cmd ;; Got answer: ;; - \u003e\u003e HEADER<<- opcode: QUERY, status: NOERROR, id: 5252 ;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;ya.ru. IN A ;; ANSWER SECTION: ya.ru. 4194 IN A 87.250.251.3 ya.ru. 4194 IN A 93.158.134.3 ya.ru. 4194 IN A 213.180.204.3 ya.ru. 4194 IN A 77.88.21.3 ya.ru. 4194 IN A 87.250.250.3 ;; Query time: 0 msec ;; SERVER: 192.168.2.9#53(192.168.2.9) ;; WHEN: Wed Nov 10 14:33:37 2010 ;; MSG SIZE rcvd: 103

and by running:

Dig ya.ru | sed "0, / ANSWER SECTION: / d; / ^ $ / q"

Ya.ru. 4160 IN A 93.158.134.3 ya.ru. 4160 IN A 213.180.204.3 ya.ru. 4160 IN A 77.88.21.3 ya.ru. 4160 IN A 87.250.250.3 ya.ru. 4160 IN A 87.250.251.3

    0, / ANSWER SECTION: / d - deletes all stream lines from the first to the line containing "ANSWER SECTION:", inclusive

    / ^ $ / q - as soon as an empty string is encountered, stop further processing.

Examples 1

Training:

Cat / etc / passwd\u003e ./test

SED Description
cat ./test | sed -e "s / systemd / SysV / g; s / Management / Unmanaged /"Apply two replacements to each line
cat ./test | sed -n "s / systemd / SysV / p"Print only the replaced strings. -n - Suppresses normal output.
cat ./test | sed "s / systemd / SysV / w ./out"Output replacement to file out
cat ./test | sed "41s / systemd / SysV /"Replace on line 41
cat ./test | sed "41,44s / systemd / SysV /"Replace in lines 41 through 44 inclusive.
cat ./test | sed "41, $ s / systemd / SysV /"Replace in lines from 41 to the last inclusive.
cat ./test | sed "/ games /, / syslog / s! / usr / sbin / nologin! / bin / bash!"Replace the lines between the lines containing games and syslog inclusive
cat ./test | sed "1c \\ DELETED"Replace first line completely
cat ./test | sed "1,5c \\ DELETED"Replace with text lines 1 through 5 inclusive
cat ./test | sed "y!: /!; \\\\!"Replace ":" with ";", and "/" with "\\"
cat ./test | sed "\u003d"Print line numbers too
cat ./test | sed -n "/ systemd / \u003d"Print the line numbers where the substring occurs
Insert the contents of the file into the stream (out):
cat ./test | sed "1rout"after 1 line
cat ./test | sed "$ r out"after the last line
cat ./test | sed "1,4rout"after 1, 2, 3 and 4 lines
cat ./test | sed "/ syslog / r out"after the line containing syslog
Delete lines:
cat ./test | sed "/ games /, / syslog / d"which are between the lines containing games and syslog inclusive
cat ./test | sed "3,5d"from 3 to 5
cat ./test | sed "5, $ d"from 5 to the end

Many of you probably used the stream text editor sed for some of your purposes, if not, I'll be glad to tell you about it, I'll try to get more details. Why is it called streaming? The answer is simple - imagine an input text document that goes through the program and as a result you get some other form of this file processed by the program. A kind of meat grinder - you put meat on the basis of the grid - you get either minced meat or something else.

So, by default, it seems like this utility should already be on your system (in my case, in Debian 7.6 I already had it), if not, then -

With text:

the "s" parameter at the beginning indicates that the text should be replaced, g - at the end of the replaced text - what needs to be done globally (throughout the file)

For example, we want to replace the word Sergey with Andrey in our text.txt file and upload all this to the textout.txt file, proceed:

sed "s / Sergey / Andrey / g" text. txt\u003e textout. txt

Result:

If you want to make substitutions for special characters - for example, for the & character, then you need before special. put a backslash "\\" with a character, if it is necessary to indicate what sed needs to turn to the beginning of a line, use the special character "^". In addition, you can write 2 or more changes on one line, separating them with a semicolon - ";". For example, we torture the already changed textout.txt file. First, I'll show you the current content of the textout.txt file again:

root @ testhostname: ~ # cat textout.txt

Test for Andrey

Test 2 for Andrey

Test 3 for Andrey

Now we enter the command:

sed "s / for / \\ & / g; s / ^ Test / Sergey / g" textout. txt\u003e textout2. txt

Thus, instead of the word for, we put the & icon (the special character is entered with the "\\" before the special character), then the separation sign (so that all changes can be written in one sed line -\u003e ";", instead of the word at the beginning of the line "Test" put the word Sergey, the result of what happened:

Everything as we wanted!

Also sed is a good helper for viewing logs. For example, we need to unload all lines of today's date (let in our case it is October 10) from the log file / var / log / messages to the testlog.txt file, let's proceed:

sed - n "/ ^ Oct 10 / p" / var / log / messages\u003e testlog. txt

here we have added the -n parameter, and then - '/ ^ Oct 10 / - meaning that the line must start from the date of October 10, then the p parameter - meaning print (print the content under this condition), then the source file and the file where we are Let's throw off the results by our filter condition, run it, and see that the testlog.txt file contains only October 10:

Excellent! If you do not need many lines, but conditionally there is a need to take only from the first to the fifth line, we separate our current query with the "|" removing the unloading into the testlog.txt file and writing sed -n 1,5p - which means that we need to print (p - print at the end of the expression) from the first "1" to (separated by commas) the fifth "5" line. In total, we get something like this:

sed - n "/ ^ Oct 10 / p" / var / log / messages | sed - n 1, 5p\u003e testlog - 5strok.txt

Once again I draw your attention to the fact that the file where we upload the results is moved to the end (testlog-5strok.txt), we see the result of our actions:

Introduction

The sed command is a Stream EDitor for automatic text editing. A "stream editor" means that it can edit an incoming data stream continuously, say, as part of a pipe. Automatic - this means that as soon as you set the rules for editing, further happens without your tedious participation. In other words, sed is not interactive.

Sed is more complex than the commands we've covered in previous articles in the HuMan series. It has an arsenal of its own commands, so to avoid tautology and confusion, in this article the sed command will henceforth be referred to as the "program" or "editor", and the sed editor commands are simply commands.

The sed program is capable of complex tasks, and it takes time to learn how to formulate these tasks.

But along with the complex operations, sed has simple yet useful features that are no more difficult to master than other Unix commands. Due to the complexity of mastering the entire program, do not allow yourself to abandon its simple aspects.

We'll start from easy to hard so you can always figure out where to stop.

S command - substitution

The sed program has many commands of its own. Most users only know the s command, which is enough to get started with sed. The s command replaces SAMPLE with REPLACEMENT:

sed s / SAMPLE / REPLACE /

$ echo day | sed s / day / night / (Enter) night

It couldn't be easier. And here's an example with input from zar.txt:

In the morning, he did exercises. Lightning is an electrical charge. $ sed s / charge / discharge / zar.txt In the morning he did a discharge. Lightning is an electrical discharge.

I did not put the expression s / SAMPLE / REPLACE / in quotation marks, since this example does not need quotation marks, but if there were metacharacters in it, then quotation marks would be required. In order not to break your head every time, and not to make a mistake inadvertently, always put quotes, better "strong" single, this is a good habit. You can't spoil porridge with butter. I, too, in all subsequent examples will not skimp on quotes.

As we can see, the replacement command s has four components:

S command itself /.../.../ delimiter PATTERN pattern to search for and then replace REPLACE expression that will replace the PATTERN, if found.

The forward slash (/) is traditionally used as a separator, since sed's ancestor, ed, uses them (as does vi). In some cases, such a separator is very inconvenient, for example, when it is necessary to change the paths to directories that also contain a forward slash (/ usr / local / bin). In this case, you have to separate forward slashes by backslashes:

Sed "s / \\ / usr \\ / local \\ / bin / \\ / common \\ / bin /"

This is called "palisade" and looks very ugly, and most importantly, incomprehensible.

What is unique about sed is that it allows any separator, such as the underscore:

$ echo day | sed s_day_night_night

or colon:

$ echo day | sed s: day: night: night

If you get the message "incomplete command` s "" while looking for a delimiter you like, then that character is not a good delimiter, or you simply forgot to put one or two delimiters.

In this article, I have to use the traditional separator (/) so as not to confuse the reader, but if necessary, I will use the tilde (~) as the separator.

Regular Expressions (Rs)

(Regular expressions, regexp, RE)

The topic of regular expressions is so vast that entire books have been devoted to it (see links at the end of the article). However, talking seriously about sed without using regular expressions is just as counterproductive as talking about trigonometry with counting sticks. Therefore, it is necessary to talk about at least those regular expressions that are often used with the sed program.

from Or any other letter. Most letters, numbers, and other non-special characters are considered to represent themselves as regular expressions.

* An asterisk following any character or regular expression means any number (including zero) repeats of this character or regular expression.

\+ Indicates one or more repetitions of a character or regular expression.

\? Means none or one repetition.

\\ (i \\) Means exactly i repetitions.

\\ (i, j \\) The number of repetitions is in the range from i to j inclusive.

\\ (i, \\) The number of repetitions is greater than or equal to i.

\\ (, j \\) The number of repetitions is less than or equal to j.

) Remember the regular expression or its part for further use as a whole. For example, \\ (a-z \\) * will search for any combination of any number (including zero) lowercase letters.

. Means any character, including newline character.

^ Indicates a null expression at the beginning of a line. In other words, what this character is preceded must appear at the beginning of the line. For example, ^ # include will search for lines starting with #include.

$ Same as above, only applies to the end of the line.

[LIST] Means any character from the LIST. For example, will search for any English vowel.

[^ LIST] Means any character other than those in the list. For example, [^ aeiou] will match any consonant. Note: LIST can be an interval, for example [a-z], which means any lowercase letter. If it is necessary to include in the LIST] (square bracket), indicate it in the list first; if you need to include in the LIST - (hyphen), then indicate it in the list first or last.

RE1 \\ | RE2 Means PB1 or PB2.

RE1RE2 Means the union of the regular expressions PB1 and PB2.

\\ n Indicates a newline character.

\$; \*; \.; \[; \\; \^ Mean, respectively: $; *; .; [; \; ^

Attention: Other backslash (\\) -based conventions in C are not supported by sed.

\1 \2 \3 \4 \5 \6 \7 \8 \9 Indicates the corresponding part of the regular expression, memorized using the \\ (and \\) characters.

A few examples:

abcdef Means abcdef

a * b Means zero or any number of letters a and one letter b. For example, aaaaaab; ab; or b.

a \\? b Means b or ab

a \\ + b \\ + Means one or more letters a and one or more letters b. For example: ab; aaaab; abbbbb; or aaaaaabbbbbbb.

.* Means all characters on a line, on all lines, including blank lines.

.\+ Means all characters on a string, but only on strings that contain at least one character. Blank lines do not match the given regular expression.

^ main. * (. *) It will search for lines starting with the word main, as well as those containing opening and closing brackets, and any number of characters (or not) may appear before and after the opening parenthesis.

^# Will search for lines starting with a # sign (eg comments).

\\$ Will search for lines ending with a backslash (\\).

Any letters or numbers

[^ ]\+ (The square bracket, in addition to the ^ character, also contains a space and a tab.) - Means one or any number of any characters other than a space and a tab. Usually the word is meant.

^. * A. * $ Indicates the capital letter A exactly in the middle of the line.

A. \\ (9 \\) $ Indicates a capital A, exactly the tenth from the end of the line.

^. \\ (, 15 \\) A Indicates the capital letter A, exactly the sixteenth from the beginning of the line.

Now that we are familiar with some regular expressions, let's go back to the s command of sed.

Using the & symbol when the SAMPLE is unknown "How is this unknown?" You ask, "Don't you know what you want to replace?" The answer is: I want to put in brackets any numbers found in the text. How to do it? Answer: use the & symbol.

The & (ampersand) symbol, when placed in a SUBSTITUTION, denotes any SAMPLE found in the text. For instance:

$ echo 1234 | sed "s / * / (&) /" (1234)

An asterisk (asterisk) after the interval is needed to replace all the numbers found in the sample. Without it, it would have turned out:

$ echo 1234 | sed "s // (&) /" (1) 234

That is, the very first figure found was taken as a sample.

Here's an example with a very meaningful load: let's create a file formula.txt:

A + 432-10 \u003d n

and apply the command to it:

$ sed "s / * - * / (&) /" formula.txt a + (432-10) \u003d n

The mathematical formula has acquired an unambiguous meaning.

Another ampersand character can be used to double the SAMPLE:

$ echo 123 | sed "s / * / & & /" 123 123

There is one subtlety here. If we complicate the example a little:

$ echo "123 abc" | sed "s / * / & & /" 123 123 abc

as you would expect, only the numbers are doubled as there are no letters in the SAMPLE. But if we swap parts of the text in places:

$ echo "abc 123" | sed "s / * / & & /" abc 123

then no doubling of the numbers will work. This is a feature of the * regular expression - it only matches the first character of the string. If we want to double digits wherever they are, we need to modify the regular expression in REPLACE:

$ echo "abc defg 123" | sed "s / * / & & /" abc defg 123 123

then the numbers will double, regardless of the number of preceding "words".

Using the \\ (, \\) and \\ 1 Conditional Characters to Process a Portion of a PATTERN The \\ (and \\) (escaped parentheses) characters are used to memorize part of a regular expression.

The conventional character \\ 1 means the first remembered part, \\ 2 - the second, and so on, up to nine memorized parts (the program does not support anymore). Let's look at an example:

$ echo abcd123 | sed "s / \\ (* \\). * / \\ 1 /" abcd

Here \\ (* \\) means that the program must remember all alphabetic characters in any quantity; . * means any number of characters after the first memorized part; a \\ 1 means that we want to see only the first memorized part. Indeed, in the output of the program we see only letters and no numbers.

In order to swap words, you need to memorize two sub-SAMPLES, and then swap them:

$ echo stupid penguin | sed "s / \\ ([a-z] * \\) \\ ([a-z] * \\) / \\ 2 \\ 1 /" stupid penguin

Here, \\ 2 means the second sub-PATTERN, and \\ 1 means the first. Note the spacing between the first expression \\ ([a-z] * \\) and the second expression \\ ([a-z] * \\). It is necessary for two words to be found.

The \\ 1 character does not have to be only in REPLACEMENT, it can also be present in the SAMPLE, for example, when we want to remove duplicate words:

$ echo penguin penguin | sed "s / \\ ([a-z] * \\) \\ 1 / \\ 1 /" penguin

S command replacement modifiers

Replacement modifiers are placed after the last delimiter. These modifiers determine the actions of the program in case more than one match with the SAMPLE was found in the string, and how to make the replacement.

Modifier / g

Global replacement

Sed, like most Unix utilities, reads one line at a time when working with files. If we order to replace a word, the program will replace only the first word that matches the PATTERN on the given line. If we want to change every word that matches the pattern, then we must enter the modifier / g.

Without the / g modifier:

$ echo this cat was an ordinary cat | sed "s / cat / kitten /" this kitten was the most ordinary cat

The editor only replaced the first word that matched.

And now with the global replace modifier:

$ echo this cat was an ordinary cat | sed "s / cat / kitten / g" this kitten was the most ordinary kitten

All matches in this line have been replaced.

And if you need to change all the words, say, put them in brackets? Then regular expressions come to the rescue again. To select all alphabetic characters, both upper and lower case, you can use the construction [A-Ya-z], but it will not include words such as "something" or "with" drive ". Much more convenient is the construction [^ ] *, which matches all characters except a space. So:

$ echo stupid penguin shyly hides | sed "s / [^] * / (&) / g" (stupid) (penguin) (timidly) (hides)

How to choose the right match from several

If you do not use modifiers, sed will only replace the first word that matches the PATTERN. If you apply the modifier / g, then the program will replace each matched word. And how can you select one of the matches if there are several of them on the line? - With the help of the conventional symbols \\ (and \\) we already know, remember the sub-SAMPLES and select the one you need using the \\ 1 - \\ 9 characters.

$ echo stupid penguin | sed "s / \\ ([a-z] * \\) \\ ([a-z] * \\) / \\ 2 /" penguin

In this example, we memorized both words, and by putting the second (penguin) in the first place, the first (stupid) was removed by putting a space in the REPLACEMENT section. If we put any word instead of a space, then it will replace the first (stupid):

$ echo stupid penguin | sed "s / \\ ([a-z] * \\) \\ ([a-z] * \\) / \\ 2 smart /" penguin smart

Numeric modifier

This is a one / two / three-digit number that is placed after the last separator and indicates which match should be replaced.

$ echo very stupid penguin | sed "s / [a-z] * / good / 2" very good penguin

In this example, each word is a match, and we have told the editor which word we want to replace by placing modifier 2 after the REPLACEMENT section.

You can combine the numeric modifier with the / g modifier. If you want to leave the first word unchanged, and replace the second and subsequent ones with the word "(deleted)", then the command will be like this:

$ echo very stupid penguin | sed "s / [a-z] * / (deleted) / 2g" very (deleted) (deleted)

If you really need to delete all subsequent matches, except for the first one, then put a space in the REPLACEMENT section:

$ echo very stupid penguin | sed "s / [a-z] * / / 2g" is very

Or put nothing at all:

$ echo very stupid penguin | sed "s / [^] * // 2g" is very

The numeric modifier can be any integer from 1 to 512. For example, if you need to put a colon after 80 characters of each line, the command will help:

$ sed "s /./&:/ 80" filename

Modifier / p - print to standard output (print)

Sed already prints the output to standard output (such as a monitor screen) by default. This modifier is used only with the sed -n option, which just blocks the output of the result to the screen.

/ W modifier

Allows writing text processing results to the specified file:

$ sed "s / SAMPLE / REPLACE / w filename

/ E modifier (GNU extension)

Allows you to specify a shell command (not a sed program) as a REPLACE. If a match to the PATTERN is found, it will be replaced with the output of the command specified in the REPLACEMENT section. Example:

$ echo night | sed "s / night / echo day / e" day

/ I and / i Modifiers (GNU Extension)

Make the replacement process case insensitive.

$ echo Night | sed "s / night / day / i" day

Combinations of modifiers

Modifiers can be combined when it makes sense. In this case, you should put the w modifier last.

Conventions (GNU Extension) There are only five of them:

\\ L converts REPLACE characters to lowercase \\ l converts the next REPLACE character to lowercase \\ U converts REPLACE characters to uppercase \\ u converts the next REPLACE character to uppercase \\ E cancels a translation started by \\ L or \\ U For obvious reasons, these conventions are used alone. For instance:

$ echo stupid penguin | sed "s / stupid / \\ u & /" Stupid penguin

$ echo little puppy | sed "s / [a-z] * / \\ u & / 2" little Puppy

We have covered almost every aspect of sed's s command. Now it's time to consider the options for this program.

Sed options

The program has surprisingly few options. (Which somewhat compensates for the excess of commands, modifiers and other functions). In addition to the well-known options --help (-h) and --version (-V), which we will not consider, there are only three of them:

The -e option --expression \u003d commandset

One way to run multiple commands is with the -e option. For instance:

Sed -e "s / a / A /" -e "s / b / B /" filename

All of the previous examples in this article did not require the -e option just because they contained one command. We could have put the -e option in the examples, it wouldn't have changed anything.

The -f option If you need to execute a large number of commands, it is more convenient to write them to a file and use the -f option:

Sed -f sedscript filename

Sedscript here is the name of the file containing the commands. This file is called the sed script (hereinafter simply the script). Each script command must be on a separate line. For instance:

# comment - This script will change all lowercase vowels to uppercase s / a / A / g s / e / E / g s / i / I / g s / o / O / g s / u / U / g

You can name the script whatever you like, it is important not to confuse the script file with the file being processed.

Option -n The sed -n program does not print anything to standard output. You need a special instruction to get the output. We have already seen the / p modifier, which can be used to specify this. Let's remember the file zar.txt:

$ sed "s / 1-9 / & / p" zar.txt He did exercises in the morning. Lightning is an electrical charge.

Since no matches were found with the PATTERN (there are no numbers in the file), the s command with the / p modifier and the & sign as REPLACE (remember that the ampersand means the PATTERN itself) works like the cat command.

If the PATTERN is found in the file, the lines containing the PATTERN will be doubled:

$ sed "s / exercise / & / p" zar.txt He did exercises in the morning. In the morning he did exercises. Lightning is an electrical charge.

Now let's add the -n option:

$ sed -n "s / charge / & / p" zar.txt He did exercises in the morning.

Now our program works like a grep command - only returns lines containing a PATTERN.

Selecting the desired elements of editable text

With just one s command, we have seen the extraordinary power of sed. But all he does is to find and replace. And in the process, sed edits each line one by one, not paying attention to others. It would be convenient to limit the range of lines to be changed, for example:

  • Select rows by numbers
  • Select lines in a certain range of numbers
  • Select only lines containing an expression
  • Select only lines between some expressions
  • Select only lines from the beginning of the file to some expression
  • Select only lines from some expression to the end of the file

The sed program can do all of this and more. Any command of the sed editor can be used addressing, in a certain range of addresses, or with the above restrictions on the circle of lines. The address or restriction must immediately precede the command:

Sed "address / restriction command"

Selecting rows by numbers

This is the simplest case. We just specify the number of the required line before the command:

$ sed "4 s / [a-z] * // i" gumilev.txt What a strange bliss In the early twilight of the morning, In the melting of spring snow, to all that perishes and wisely.

$ sed "3 s / B / (B) /" gumilev.txt What a strange bliss In the early twilight of the morning, (B) the spring snow melts, In everything that perishes and is wise.

Selecting rows in a range of numbers

The range is indicated, not surprisingly, separated by commas:

$ sed "2,3 s / B / (B) /" gumilev.txt What a strange bliss (B) the early twilight of the morning, (B) the melting of spring snow, In everything that perishes and is wise.

If you need to specify a range to the last line of the file, and you do not know how many lines there are, then use the $ sign:

$ sed "2, $ s / in / (in) / i" gumilev.txt What a strange bliss (in) the early twilight of the morning, (in) the melting of spring snow, (in) about everything that perishes and wisely.

Selecting lines containing an expression

The desired expression is enclosed in forward slashes (/) and placed before the command:

$ sed "/ am / s / in / (in) / i" gumilev.txt What a strange bliss (in) the early twilight of the morning, In the melting of spring snow, In everything that perishes and is wise.

Selecting Rows Between Two Expressions

As in the case of line numbers, the range is separated by commas:

$ sed "/ am /, / wisely / s / in / (in) / i" gumilev.txt What a strange bliss (in) the early twilight of the morning, (in) the melting of spring snow, (in) about everything that perishes and wisely ...

Selecting lines from the beginning of the file to some expression

$ sed "1, / snow / s / in / (in) / i" gumilev.txt What a strange bliss (in) the early twilight of the morning, (in) the melting of spring snow, In everything that perishes and is wise.

Selecting lines from some expression to the end of the file

$ sed "/ snow /, $ s / in / (in) / i" gumilev.txt What a strange bliss In the early twilight of the morning, (in) the spring snow melts, (in) about everything that perishes and wisely.

Other sed commands

D (delete) command

Removes the specified lines from standard output:

$ sed "2 d" gumilev.txt What a strange bliss In the melting of spring snow, In everything that perishes and is wise.

And more often they write easier (without a space):

Sed "2d" gumilev.txt

Everything that was said in the previous section about addressing lines is also true for the d command (as with almost all sed commands).

Using the d command is convenient to throw away an unnecessary header of some mail message:

$ sed "1, / ^ $ / d" filename

(Delete lines from first to first blank line).

Get rid of comments in the config file:

$ sed "/ ^ # / d" /boot/grub/menu.lst

And you never know where you need to delete extra lines!

P (print) command

The English word "print" is translated as "to print", which in Russian is associated with a printer, or at least a keyboard. In fact, this word in the English context often means just displaying on the monitor screen. So the p command doesn't print anything, it just prints the specified lines to the screen.

On its own, the p command doubles the lines in the output (after all, sed prints a line to the screen by default, and p prints the same line a second time).

$ echo I have a cat | sed "p" I have a cat I have a cat

There are uses for this property, for example, to double blank lines to improve the appearance of text:

$ sed "/ ^ $ / p filename

But the p command reveals its true identity in combination with the -n option, which, as you remember, disallows the output of lines to the screen. By combining the -n option with the p command, you can get only the lines you want in the output.

For example, view lines 1 through 10:

$ sed -n "1,10 p" filename

Or just comments:

$ sed -n "/ ^ # / p" /boot/grub/menu.lst # GRUB configuration file "/boot/grub/menu.lst". # generated by "grubconfig". Sun 23 Mar 2008 21:45:41 # # Start GRUB global section # End GRUB global section # Linux bootable partition config begins # Linux bootable partition config ends # Linux bootable partition config begins # Linux bootable partition config ends

This is very similar to the grep program we already encountered when we talked about the -n option with the / p modifier. But, unlike the grep command, the sed editor makes it possible not only to find these lines, but also to change them, replacing, for example, everywhere Linux with Unix:

$ sed -n "/ ^ # / p" /boot/grub/menu.lst | sed "s / Linux / Unix /" # GRUB configuration file "/boot/grub/menu.lst". # generated by "grubconfig". Sun 23 Mar 2008 21:45:41 # # Start GRUB global section # End GRUB global section # Unix bootable partition config begins # Unix bootable partition config ends # Unix bootable partition config begins # Unix bootable partition config ends

Command!

Sometimes it is necessary to edit all lines, except for those that correspond to the SAMPLE, or selection. The exclamation point symbol (!) Inverts the selection. For example, let's delete all lines except the second from Gumilyov's quatrain:

$ sed "2! d" gumilev.txt In the early twilight of the morning,

Or select all lines except comments from the /boot/grub/menu.lst file:

$ sed -n "/ ^ # /! p" /boot/grub/menu.lst default 1 timeout 20 gfxmenu (hd0,3) / boot / message title SuSe on (/ dev / hda3) root (hd0,2) kernel / boot / vmlinuz root \u003d / dev / hda3 ro vga \u003d 773 acpi \u003d off title Linux on (/ dev / hda4) root (hd0,3) kernel / boot / vmlinuz root \u003d / dev / hda4 ro vga \u003d 0x317

Q (quit) command

The q command terminates sed after the specified line. This is convenient if you need to stop editing after reaching a certain place in the text:

$ sed "11 q" filename

This command will finish when it reaches the 11th line.

The q command is one of the few sed commands that does not accept line ranges. The command cannot stop working 10 times in a row if we enter:

Sed "1,10 q" Absurd!

W (write) command

Like the w modifier of the s command, this command allows the output of a program to be written to a file:

$ sed -n "3, $ w gum.txt" gumilev.txt

We will get a file gum.txt containing the last two lines of Gumilev's quatrain from the file gumilev.txt. Moreover, if such a file already exists, it will be overwritten. If you do not enter the -n option, then the program, in addition to creating the gum.txt file, will also display the entire contents of the gumilev.txt file.

For command line work, it is more convenient to use normal output redirection (\u003e or \u003e\u003e), but in sed scripts, the w command is likely to be useful.

R (read) command

This command not only reads the specified file, but also inserts its contents into the desired location in the file being edited. To select the "right place", we use the already familiar addressing (by line numbers, by expressions, etc.). Example:

$ echo From Gumilyov's poem: | sed "r gumilev.txt"

From a poem by Gumilyov:

What a strange bliss In the early twilight of the morning, In the melting of spring snow, In everything that perishes and is wise.

Command \u003d

Will give the number of the specified line:

$ sed "/ snow / \u003d" gumilev.txt What a strange bliss In the early twilight of the morning, 3 In the melting of spring snow, In everything that perishes and is wise.

$ sed -n "/ snow / \u003d" gumilev.txt 3

The command accepts only one address, does not accept intervals.

Y command

This command replaces symbols from the PATTERN section with symbols from the REPLACE section, working as a program tr.

$ echo Car - a legacy of the past | sed "y / Auto / Paro /" Ferry - a legacy of the past

Command y works only if the number of characters in the PATTERN is equal to the number of characters in REPLACE.

Sed scripts

In order to use sed as a full text editor, you need to become familiar with sed scripting. The sed program has its own uncomplicated programming language that allows you to write scripts that can work wonders.

This article cannot contain descriptions of sed scripts, just as its author does not set himself the task of mastering the sed programming language. In this article, I have focused on using the sed editor on the command line, with an eye on using it as a filter on pipes. For this reason, I have omitted numerous sed commands that are only used in sed scripts.

There are many fans of the sed editor, and many articles on the topic of scripting, including on the Russian Internet. So for those interested in this wonderful program, it will not be difficult to replenish their knowledge.

Sed program and Cyrillic characters

As you can see from the examples in this article, sed on a properly Russified system is fluent in the "great and mighty" language.

Summary of sed program

Sed is a powerful data stream editor, indispensable for:

  • Editing large text arrays
  • Editing files of any size when the editing sequence is too complex
  • Editing data as it becomes available, including in real time - that is, in cases where it is difficult or completely impossible to use interactive text editors.

It will take weeks or even months of work to master the sed program in full, as it requires:

  • Learn Regular Expressions
  • Learn to write sed scripts by mastering the simple programming language used in these scripts

On the other hand, some of the most common sed commands are no more difficult to master than any Unix command; I hope this article will help you with that.

Afterword

Until now, in the articles of the HuMan cycle, I tried to at least briefly reveal each option, each parameter of the described command, so that the article could replace mana. In the future, I will continue to adhere to this principle.

This article is an exception, since it does not describe all the features of the program. To fully describe them would require not an article, but a book. However, this article will give you an idea of \u200b\u200bthe sed editor and get started with this amazing program using its most common commands.

Sed is a lightweight (binary weighs only 128 kilobytes) and convenient text processing tool.

In this article, I will provide some simple examples of use sed and tell you about its main features.

Sed takes an input stream or file line by line, edits each line according to the rules defined in the sed script, and then prints the result. Sed is a Turing complete programming language.

Sed command format

The sed command has the format:

sed [-n] [-e script] [-f script file] [files]

Flag -n suppresses output
-e - indicates a list of instructions given on the command line.
-f - indicates the location of the script file.

Format of editing commands

The script file consists of a set of commands:

[address [, address]] command [arguments]

one on each line.
Addresses are either line numbers, special characters, or a regular expression:

$ - last line
start ~ N - Each N-th line starting from number start
/regular expression/ - strings matching regex
Examples:

1~2 - Every second line / REGEXP / - all lines that contain / REGEXP / 10,20 - lines 10 to 20 10,+10 - lines 10 to 20 5, ~ N - lines from the 5th to the first multiple N 5, / REGEXP / - lines containing / REGEXP /, after the 5th (not including the 5th)
  • If no address is specified, all lines are processed.
  • If one address is specified, the corresponding line is processed
  • If two addresses are specified, then lines in the specified interval are selected.
  • !command - performed command, for lines that were not selected by address.

Basic commands

Let's consider the main commands:

[address] a text - add a new line with text after the specified line

$ cat sed_test sed_test_1 11111 sed_test_2 22222 sed_test_3 33333 $ sed -e "2 a new_line" sed_test sed_test_1 11111 sed_test_2 22222 new_line sed_test_3 33333

[address [, address]] c text - Deletes the selected lines and replaces them with text

$ sed -e "2 s new_line" sed_test sed_test_1 11111 new_line sed_test_3 33333 $ sed -e "/ 3 / s new_line" sed_test sed_test_1 11111 sed_test_2 22222 new_line

[address [, address]] d - Deletes the specified lines.

$ sed -e "2 d" sed_test sed_test_1 11111 sed_test_3 33333 $ sed -e "2! d" sed_test sed_test_2 22222

[address] i text - Paste text in place of the specified line.

$ sed -e "2 i new_line" sed_test sed_test_1 11111 new_text sed_test_2 22222 sed_test_3 33333

[address [, address]] p (with flag -n) displays the found lines.

$ sed -ne "2p" sed_test sed_test_2 22222

[address] q - exit sed.

[address [, address]] r file - Is reading file and issues its content to the exit.

[address [, address]] s / regex / replace / flags - Replaces regular expression on replacement-y taking into account the flags:

  • g - in the whole line
  • i - case insensitive
  • p - display the replacement result
$ sed -ne "s / t / T / g" sed_test sed_TesT_1 11111 sed_TesT_2 22222 sed_TesT_3 33333 $ sed -e "s // d / g" sed_test sed_test_d ddddd sed_test_d ddddd sed_test_d ddddd

[address [, address]] y / line1 / line2 / - Replaces all occurrences of characters in line1 matching characters from strings2... Line lengths must be the same.

$ sed -ne "y / est / EST / g" sed_test SEd_TEST_1 11111 SEd_TEST_2 22222 SEd_TEST_3 33333

[address [, address]] (commands) - brackets group commands
[address] \u003d - Gives line numbers

Tags

: label - match a group of teams label
b label label, if label missing, then jump to the end of the batch file.

t label - jump to the command indicated by the label label only after a successful replacement using the command s ///

Execution cycle

sed operates on two data buffers: main and auxiliary. Initially, both buffers are empty.
Work with these buffers is carried out using the commands: \\\\ `h’, `H’, `x’, `g’, `G’ D ’ h - Replace the contents of the auxiliary buffer with the contents of the main
H - Add a new line to the auxiliary buffer and then append the contents of the main buffer to the contents of the auxiliary
x - Swap the contents of both buffers in places
g - Replace the contents of the main buffer with the contents of the auxiliary
G - Add a new line to the main buffer and then append the contents of the auxiliary buffer to the contents of the main
D - Delete the text of the main buffer to the next line feed character
N - Add a new line to the main buffer, then add the next processed line there
P - Output the contents of the main buffer up to the next line feed character.

More complex examples

The following script swaps the lines of the file (the first lines become the last and vice versa)

$ cat tac.sed #! / usr / bin / sed -nf # starting with the second line, the contents of the buffer (which already contains # all previous lines) is appended to the current line. 1! G # when the last line is reached, we print $ p # We write the data into the buffer again h sed -nf tac.sed sed_test sed_test_3 33333 sed_test_2 22222 sed_test_1 11111

We read the lines of the file (display the number of the last line)

$ cat count.sed #! / usr / bin / sed -nf $ \u003d

result

$ sed -nf count.sed sed_test 3

Reversing strings

$ cat revers.sed #! / usr / bin / sed -f # skip single-letter lines /../! b # Reverse the string. Add an empty line before and after the current one. s /% [email protected]~ *! G4;:% # `. * $ / \\ & \\ / # Move the first character to the end # the loop runs as long as there are characters in the middle line. tx: xs / \\ (\\\\ n. \\) \\ (. * \\) \\ (. \\\\ n \\) / \\\\ 3 \\\\ 2 \\\\ 1 / tx # remove unnecessary line breaks s / \\\\ n // g

This script moves two letters at a time.

$ sed -f revers.sed sed_test 11111 1_tset_des 22222 2_tset_des 33333 3_tset_des

Additional Information

You can learn more about the format of sed scripts by reading the manual. man sed or technical documentation info sed.


Author: Rares Aioanei
Publication date: November 19, 2011
Translation: A. Krivoshey
Date of transfer: July 2012

1. Introduction

Welcome to the second part of our series on sed, the GNU version. There are several versions of sed that are available on different platforms, but we will focus on GNU sed 4.x. Many of you have heard of, or have already used sed, most likely as a replacement tool. But this is just one of the uses of sed, and we will try to show you all aspects of using sed. Its name stands for "Stream EDitor" and the word "stream" in this case can mean a file, a channel, or simply stdin. We hope that you already have a basic knowledge of Linux, and if you have already worked with regular expressions, or at least know what it is, then everything will be much easier for you. The length of this article does not allow us to include a complete guide to regular expressions; instead, we will explain the basic concepts and provide a large number of examples of using sed.

2. Installation

There is not much to tell here. Most likely you already have sed installed, as it is used by various system scripts, as well as by Linux users who want to improve their efficiency. You can find out what version of sed you have installed with the command:

$ sed --version

On my system, this command shows that I have GNU sed 4.2.1 installed plus gives a link to the program's home page and other useful information. The package is named "sed" regardless of distribution, except for Gentoo, where it is implicitly present.

3. Concepts

Before going any further, we think it is important to focus on what sed does, since the word "stream editor" says little about its purpose. sed accepts text as input, performs the specified operations on each line (unless otherwise specified), and prints the modified text. The specified operations can be add, insert, delete or replace. This is not as easy as it sounds: be warned that there are a large number of options and their combinations that can make the sed command very difficult to understand. Therefore, we recommend that you learn the basics of regular expressions to understand how it works. Before starting the tutorial, we would like to thank Eric Pement and others for the inspiration and what he has done for everyone who wants to learn and use sed.

4. Regular expressions

Since sed commands (scripts) remain a mystery to many, we feel that our readers should understand the basic concepts and not blindly copy and paste commands whose meaning they do not understand. When a person wants to understand what regular expressions are, the keyword is "match", or more precisely, "match pattern". For example, in a report for your department, you wrote the name Nick to a network architect. But Nick is gone and John is in his place, so now you have to replace Nick with John. If the report file is named report.txt, you must run the following command:

$ cat report.txt / sed "s / Nick / John / g"\u003e report_new.txt

Sed uses stdout by default, you can use the output redirection operator as shown in the example above. This is a very simple example, but we have illustrated a few points: we look for all matches to the pattern "Nick" and replace in all cases with "John". Note that sed does case sensitive searches, so be careful and check the output file to make sure all replacements have been made. The above example could have been written like this:

$ sed "s / Nick / John / g" report.txt\u003e report_new.txt

Okay, you say, but where are the regular expressions here? Yes, we wanted to show an example first, and now the fun part begins.
If you are not sure if you wrote "nick" or "Nick" and want to accommodate both, you must use the sed command "s / Nick / nick / John / g". The vertical bar has a meaning that you should know if you have studied C, that is, your expression will match "nick" or "Nick". As you will see below, the channel can be used in other ways, but the meaning is the same. Other operators commonly used in regular expressions are "?", Which matches zero or one repetition of the preceding character (that is, flavou? R will match flavor and flavor), "*" zero or more times, "+" one or more times. "^" matches the beginning of a line, and "$" does the opposite. If you are a vi or vim user, many things will sound familiar to you. After all, these utilities, along with awk and C, have their roots in the early days of UNIX. We will not talk more on this topic, since it is easier to understand the meaning of these symbols with examples, but you should be aware that there are different implementations of regular expressions: POSIX, POSIX Extended, Perl, as well as different implementations of fuzzy regular expressions that guarantee you a headache ...

5. Examples of sed usage

Command syntax Description
sed "s / Nick / John / g" report.txt Replaces every occurrence of Nick with John in report.txt
sed "s / Nick / nick / John / g" report.txt Replaces each occurrence of Nick or nick with John.
sed "s / ^ / /" file.txt\u003e file_new.txt Adds 8 spaces to the left of the text to improve print quality.
sed -n "/ Of course /, / attention you \\
pay / p "myfile
Outputs one paragraph starting with "Of course" and ending with "attention you pay"
sed -n 12.18p file.txt Only prints lines 12-18 of file.txt
sed 12,18d file.txt Outputs the entire file file.txt except for lines 12 to 18
sed G file.txt Doubles spaces in file.txt
sed -f script.sed file.txt Writes all commands to script.sed and executes them.
sed "5! s / ham / cheese /" file.txt Replaces ham with cheese in file.txt except for line 5
sed "$ d" file.txt Removes the last line
sed "/ \\ (3 \\) / p" file.txt Prints only lines with three consecutive digits
sed "/ boom /! s / aaa / bb /" file.txt If "boom" is found, replace aaa with bb
sed "17, / disk / d" file.txt Deletes all lines from line 17 to "disk"
echo ONE TWO / sed "s / one / unos / I" Replaces one with unos case-insensitively, so "unos TWO" will be printed
sed "G; G" file.txt Triple spaces in a file
sed "s /.$//" file.txt A way to replace dos2unix :)
sed "s / ^ [^ t] * //" file.txt Removes all spaces before every line in file.txt
sed "s / [^ t] * $ //" file.txt Removes all spaces at the end of each line in file.txt
sed "s / ^ [^ t] * //; s / [^] * $ //" file.txt Removes all spaces at the beginning and end of each line in file.txt
sed "s / foo / bar /" file.txt Replaces foo with bar only at the first occurrence of a line.
sed "s / foo / bar / 4" file.txt Replaces foo with bar only at the fourth occurrence of a line.
sed "s / foo / bar / g" file.txt Replaces foo with bar for all occurrences of the string.
sed "/ baz / s / foo / bar / g" file.txt Replace foo with bar only if the string contains baz.
sed "/./,/^$/!d" file.txt Remove all consecutive blank lines except EOF
sed "/ ^ $ / N; / \\ n $ / D" file.txt Remove all consecutive blank lines, but leave the top blank line.
sed "/./,$!d" file.txt Remove all leading blank lines
sed -e: a -e "/ ^ \\ n * $ / ($ d; N;); / \\ n $ / ba" \\
file.txt
Remove all trailing blank lines
sed -e: a -e "/ \\\\ $ / N; s / \\\\\\ n //; ta" \\
file.txt
If the file ends with a backsplash, concatenate it with the following (useful for shell scripts)
sed "/ regex /, + 5 / expr /" Matches regex plus 5 next lines
sed "1 ~ 3d" file.txt Delete every third line starting from the first.
sed -n "2 ~ 5p" file.txt Print every fifth line, starting with the second.
sed "s / ick / John / g" report.txt Another way to write some of the above examples. Can you suggest yours?
sed -n "/ RE / (p; q;)" file.txt Print only the first RE (regular expression) match
sed "0, / RE / (// d;)" file.txt Removes only the first match
sed "0, / RE / s // to_that /" file.txt Changes only the first match
sed "s / ^ [^,] *, / 9999, /" file.csv Replaces the first field with 9999 in the CSV file
s / ^ * \\ (. * [^] \\) * $ // \\ 1 //; s / "*, * /" // g; : loop s // * \\ ([^ ", /] [^, /] * \\) *, * // \\ 1 // g; s // *, * // \\ 1 // g; t loop s / * //// g; s // * /// g; s / ^ / \\ (. * \\) / $ / \\ 1 /; A sed script to convert a CSV file to a file with a pipe separator (only works with some types of CSV, with embedded quotes and commas).
sed ": a; s / \\ (^ \\ / [^ 0-9.] \\) \\ (\\ + \\) \\ (\\ (3 \\) \\) / \\ 1 \\ 2, \\ 3 / g; ta" file .txt Changes the format of numbers in file.txt from 1234.56 to 1.234.56
sed -r "s / \\<(reg/exp)+/\U&/g" Converts any word beginning with reg or exp to uppercase.
sed "1.20 s / Johnson / White / g" file.txt Replace Johnson with White on lines 1 - 20 only.
sed "1,20! s / Johnson / White / g" file.txt The previous example is the other way around (replaces everywhere except lines 1-20)
sed "/ from /, / until / (s / \\ / magenta / g; \\ s / \\ / cyan / g;)" file.txt Replaces only between "from" and "until"
sed "/ ENDNOTES: /, $ (s / Schaff / Herzog / g; \\ s / Kraft / Ebbing / g;)" file.txt Replaces only from the word "ENDNOTES:" to EOF
sed "/./(H;$!d;);x;/regex/!d" file.txt Print a paragraph only if it contains a regex
sed -e "/./(H;$!d;)" -e "x; / RE1 /! d; / RE2 /! d; / RE3 /! d" file.txt Prints paragraphs only if they contain RE1, RE2 and RE3
sed "s / 14" / fourteen inches / g "file.txt This way you can use double quotes
sed "s / \\ / some \\ / UNIX \\ / path / \\ / a \\ / new \\ / path / g" file.txt Working with Unix paths
sed "s /// g" file.txt Removes all characters from a to g from file.txt
sed "s / \\ (. * \\) foo / \\ 1bar /" file.txt Replaces only the last match of foo with bar
sed "1! G; h; $! d" Replacing the tac command
sed "/\\n/!G;s/\\(.\\)\\(.*\\n\\)/&\\2\\1/;//D;s/.//" Replacing the rev command
sed 10q file.txt Head command replacement
sed -e: a -e "$ q; N; 11, $ D; ba" \\ file.txt Tail command replacement
sed "$! N; /^\\(.*\\)\\n\\1$/!P; D" \\ file.txt Replacing the uniq command
sed "$! N; s / ^ \\ (. * \\) \\ n \\ 1 $ / \\ 1 /; \\ t; D" file.txt Reverse command (equivalent to uniq -d)
sed "$! N; $! D" file.txt Equivalent to tail -n 2
sed -n "$ p" file.txt ... tail -n 1 (or tail -1)
sed "/ regexp /! d" file.txt Equivalent to grep
sed -n "/ regexp / (g; 1! p;); h" file.txt Prints the line before the first match of the regular expression, but not including the match
sed -n "/ regexp / (n; p;)" file.txt Prints the line after the first match of the regular expression, but does not include the match itself
sed "/ pattern / d" file.txt Deletes lines matching pattern
sed "/./!d" file.txt Removes all blank lines from the file
sed "/ ^ $ / N; / \\ n $ / N; // D" file.txt Deletes all consecutive blank lines except the first two
sed -n "/ ^ $ / (p; h;); /./ (x; /./ p;)" \\ file.txt Removes the last line of each paragraph
sed "/ ^ $ / q" Gets the email header
sed "1, / ^ $ / d" Retrieves the body of the email
sed "/ ^ Subject: * /! d; s ///; q" Gets the subject of the email
sed "s / ^ /\u003e /" Quotes the message by inserting "\u003e" in front of each line
sed "s / ^\u003e //" Reverse command (removes the quote from the message)
sed -e: a -e "s /<[^>] *\u003e // g; / Removes HTML tags
sed "/./(H;d;);x;s/\\n/\u003d(NL)\u003d/g" file.txt / sort \\ / sed "1s / \u003d (NL) \u003d //; s / \u003d ( NL) \u003d / \\ n / g " Sorts paragraphs in file.txt alphabetically
sed " [email protected]/ usr / [email protected]&/[email protected]"path.txt Replaces / usr / bin with / usr / bin / local in path.txt
sed " [email protected]^.*[email protected]<<<&>\u003e\u003e @ g "path.txt Try it and see :)
sed "s / \\ (\\ / [^:] * \\). * / \\ 1 / g" path.txt Provided path.txt contains $ PATH, only prints the first path on each line
sed "s / \\ ([^:] * \\). * / \\ 1 /" / etc / passwd Replacement awk - only show users from passwd file
echo "Welcome To The Geek Stuff" / sed \\ "s / \\ (\\ b \\) / \\ (\\ 1 \\) / g" (W) elcome (T) o (T) he (G) eek (S) tuff Understandable without explanation
sed -e "/ ^ $ /, / ^ END / s / hills / \\ mountains / g" file.txt Replaces "hills" with "mountains", but only in blocks of text that start with an empty line and end with a line with three "END" characters, inclusive.
sed -e "/ ^ # / d" / etc / services / more Shows the services file with no lines commented out
sed " [email protected]\([^:]*\):\([^:]*\):\([^:]*\)@\3:\2:\[email protected]"path.txt Reverse the order of the elements in the last line of path.txt
sed "/ regex / (x; p; x;)" file.txt Inserts a new line above each line that matches a regular expression
sed "/ AAA /! d; / BBB /! d; / CCC /! d" file.txt Searches for AAA, BBB and CCC matches in any order
sed "/AAA.*BBB.*CCC/!d" file.txt Searches for AAA, BBB and CCC matches in a given order
sed -n "/^.\\(65\\)/p" file.txt Prints lines of 65 characters or more
sed -n "/^.\\(65\\)/!p" file.txt Prints lines of 65 characters or less
sed "/ regex / G" file.txt Inserts a blank line below each line
sed "/ regex / (x; p; x; G;)" file.txt Inserts a blank line above and below each line
sed \u003d file.txt / sed "N; s / \\ n / \\ t /" Numbers lines in file.txt
sed -e: a -e "s / ^. \\ (1.78 \\) $ / & /; ta" file.txt Align text to the right
sed -e: a -e "s / ^. \\ (1,77 \\) $ / & /; ta" -e "s / \\ (* \\) \\ 1 / \\ 1 /" file.txt Align text to center

6. Conclusion

This is only a fraction of what could be said about sed, but this series is more of a hands-on guide that we hope will help you appreciate the full power of Unix utilities and make your work more efficient.