Most useful series:

The Most Common Set of “Vim” Techniques for Linux Production

The most Common set of “Sed” Techniques for Linux Production

The Most Common Set of “AWK” Tips for Linux Production

“Sed” Advanced Function: My Little Brain is spinning.”

The sed command has two Spaces, one called pattern space and one called hold space. These two Spaces demonstrate that the human brain is very small and requires a lot of training and brain-burning understanding to adapt to some very simple operations.

This simple command deletes the last two lines of a file.

sed 'N; $! P; $! D; $d' file
Copy the code

In “The Most Common Set of” Sed “techniques for Linux production,” we introduced common Sed commands and operations, aided by two diagrams. Unfortunately, these two diagrams are not strictly accurate (the S command, for example, is only a subset of them), even though they can help beginners get started quickly.

This is an intermediate use of sed. It is common in some sed scripts. It is not often used in daily life, but it can achieve unexpected results.

The principle of

Working mode

This starts with the way SED works.

with open('file'.'r') as f:
    for line in f.readlines():
        print(line)
Copy the code

The sed command buffers another thing on top of this. That’s the “previous line”, called Hold Space. The current line is called patter space. Here’s a simple expression in Python:

hold_space = ""
with open('file'.'r') as f:
    for pattern_space in f.readlines():
        print(hold_space,pattern_space)
        hold_space = pattern_space
Copy the code

The specific process is similar to the above code, for example. In this example, hold space does not participate in the operation and is completely insensitive:

1, read the current line WTF.. To the Pattern space

2. Execute the command p, which prints the current line

3. Assign the contents of Pattern space to Hold Space

4. Continue on the next line and repeat the process

One example: X

But I want to play with these two buffers a little bit. This operation is swap, denoted by x, which is also the consistent urination of some text editors.

So, before p, we added an x. Indicates that the two buffers are swapped and then moved down.

hold_space = ""
with open('file'.'r') as f:
    for pattern_space in f.readlines():
        swap(hold_space,pattern_space)
        print(hold_space,pattern_space)
        hold_space = pattern_space
Copy the code

So let me just imagine the process.

1. Initially, the contents of hold_space are empty. But the goose, before it’s filled, it’s used, and it’s swapped with the current row

The p command is used on the displaced buffer to print the blank line fuck for the first time

3, continue beep beep, now at the last line, immediately replaced, did not have a chance to print into the hold_space

4, the current line, storage is the penultimate line of data, the last line of light death, there will never be a chance to appear

There are ways to figure it out, for example, if I swap x an even number of times.

sed -n 'x; x; x; x; p' file
Copy the code

You ever feel like riding an alpaca around the world?

application

Take a chestnut

You might be thinking, well, what good do I do swapping these two buffers? Now look at this file.

Small sister taste public account CEO Garfield Manager IT Manager system destruction engineer Sysadmin Small brother taste Developer love to sell things Manager wind qingyang DogCopy the code

The odd-numbered lines of the file are names, and the even-numbered lines are positions. We want to output the names of all managers. You can use the following command.

sed -n -e 'x; n' -e '/Manager/{x; p}' file
Copy the code

The command is divided into two parts.

x; N means to store even rows in the Pattern space, and odd rows in the hold space.

/Manager/{x; The p} command performs a lookup for the Manager keyword on pattern space. If the conditions are met, the P and H buffers are swapped again, printing the names of the odd rows.

The x and n above are commands for these two buffers. There are many such orders.

The command

These commands, if too many, can be enclosed with {}, just like the commands above. These commands are located in the same place as we described in our last post.

The commonly used:

X Please allow me to install B: Exchange the contents of the hold and pattern Spaces in English.

D Clears the current pattern space and enters the next loop

D Delete the first line of pattern space

H Copy the pattern space to the hold space

H Append pattern spaced to hold space

G Copy the hold space to pattern space

G Append hold space to pattern space

N Reads the next input line into pattern space

N appends the next input line to the pattern space, treating both lines as one, but with a \ N newline character between them

P Prints the current pattern space

P Prints the first line of the current pattern space

Not commonly used

Last time we talked about pushing boxes, we used a lot of these things. To give the user real “freedom” when writing sed scripts, sed also allows tokens with “:” in scripts. Tags, a programming language-like feature.

Q exits sed to increase the execution speed

L Lists the current line, containing non-printable characters

L width Lists the current row, ending with a width character

B label jumps to the corresponding label.

T label if branches, starting at the last line, to the labeled command or to the end of the script if the condition is met or t, t command. Test the command.

The T label error branch, starting at the last line, causes an error or T, T command to branch to a labeled command, or to the end of the script.

Of course, there are other, less common ones that you can view with the man command

man sed
Copy the code

Some commands: Turn on training mode

It seems simple, doesn’t it, to look at it line by line? Impossible. Train your rusty brain with a few simple commands.

An assembly-line command

sed -n '2{h; n; x; H; x}; p' file
Copy the code

Swap lines 2 and 3

Print the last line

sed 'N; D' file
Copy the code

The last two lines in the output file

sed '$! N; $! D' file
Copy the code

Deletes the last two lines of the file

sed 'N; $! P; $! D; $d' file
Copy the code

Another way to print even lines

Sed - n'n; p' file
Copy the code

Add a blank line every five lines.

sed 'n; n; n; n; G' file
Copy the code

Prints paragraphs with AAA and BBB and CCC (in any order)

sed -e '/./{H; $! d; } ' -e 'x; /AAA/! d; /BBB/! d; /CCC/! d' file
Copy the code

Reverse line order (change last line to first line, first line to last line)

sed -n '1! G; h; $p' file
Copy the code

This command is a little convoluted. First of all, 1! G does the G operation on all the lines except the first line, and then copies back in reverse. When it reaches the last line, it prints Pattern Space directly. ${p} = ${p}

A tagged usage

sed -e :a -e '$q; N; 11,$D; ba'
Copy the code

Print the last 10 lines. A is the label. Syntax is a single line, separated by:.

End

In order to improve your competitiveness in the company, you can make a lot of SED scripts to scare people. As with some Perl scripts, even experienced developers, let alone others, need to work hard to understand them.

This is sed, a simple but not simple command, and this article is an intermediate introduction. Intermediate is also a bit of a brain drain, because your brain is always maintaining these two buffers. It’s displacement, it’s emptying, it’s the human state machine. Of course, how to explain this process as simple as possible, or waste a lot of the author’s brain cells. Even if you give her a “like”, it is a way to delay the progression of Alzheimer’s disease. With your support, little sister can also think of something other than technology, such as spraying bat.