Recently looking at the SED command of Linux, it is said to have to pattern space, hold space, edit command h, X, G a little difficult to understand, studied the following things:
There are two types of buffers for sed:
Generally speaking, sed copies the input file to the buffer, and after the copy in the buffer is processed, it is output, this buffer is called: pattern buffer (pattern space);
The other one is hold Buffer.
Let's look at how SED works in the first instance,
HowsedWorks
sedMaintains data buffers:
The active pattern space, and the auxiliary hold space. Both is initially empty.
sedOperates by performing the following cycle on each line of input:
First , sed Reads one line from the input stream, removes any trailing newline, and places it in the pattern space.
then commands is executed; Each command can has an address associated to it:addresses be a kind of condition code, and a command is only executed If the condition is verified before, the command is to be executed.
When the end of the script was reached, unless the -n option is on use, the contents of pattern space is print Ed out to the output stream, adding back the trailing newline if it is removed. Then the next cycle starts for the next input line.
After translation (the part about trailing newline is not very clear) the approximate meaning is as follows:
1. Read a line from the input stream;
2, if the matching words to execute the order;
3. If there is no-n option, print the contents of the pattern space to the output stream.
4, the next line repeats 1-3.
Description of h/h, g/g, x in man sed:
H/h:copy/append pattern space to hold space. (Copy/Append pattern space to hold space)
G/g:copy/append hold space to pattern space. (Copy/Append hold space to pattern space)
X:exchange the contents of the hold and pattern space. (Interchange pattern, hold space)
Examples come:
#cat Name.txt
1 Tom
2 Jerry
3 Selina
4 Green
5 Lily
6 Lilei
#sed-E '/tom/h '-e '/green/x '-e ' $G ' name.txt
Row by line parsing is as follows:
Article progressive |
COMMAND |
PATTERN SPACE |
Hold SPACE |
OUTPUT |
1 Tom |
/tom/ |
1 Tom |
Empty |
1 Tom |
H |
1 Tom |
1 Tom |
2 Jerry |
No |
2 Jerry |
1 Tom |
2 Jerry |
3 Selina |
No |
3 Selina |
1 Tom |
3 Selina |
4 Green |
/green/ |
4 Green |
1 Tom |
1 Tom |
X |
1 Tom |
4 Green |
5 Lily |
No |
5 Lily |
4 Green |
5 Lily |
6 Lilei |
$ |
6 Lilei |
4 Green |
6 Lilei 4 Green |
G |
6 Lilei 4 Green |
4 Green |
Therefore, the output of the command is:
#sed-E '/tom/h '-e '/green/x '-e ' $G ' name.txt
1 Tom
2 Jerry
3 Selina
1 Tom
5 Lily
6 Lilei
4 Green
Finally look at the-n option and the P command:
-n(
--quiet,--silent)
By default, the prints-out of the pattern space is at the end of each sed cycle through the script. These options disable this automatic printing, and only sed produces output when explicitly told to via the p
Comman D.
P:
Print out of the pattern space (to the standard output). This command was usually only used in conjunction with the -n command-line option.
Presumably, the-n option suppresses the automatic printing of pattern space, and p invalidates-N.
#sed-n '/selina/p ' Name.txt
3 Selina
Because rows 1th, 2, 4, 5, 6 are suppressed by-N, and the 3rd line is matched, and the P command is executed, the-n is not valid, so it can be output.
This article is from the "xiang1162090014" blog, make sure to keep this source http://xiang1162090014.blog.51cto.com/3876247/1664202
Sed-pattern space, hold space