-
The first part is a subset of concepts and examples, which are transferred from: http://coolshell.cn/articles/9104.html
-
Pattern Space
The
-
0th one is about the-n parameter, we may not understand, it's okay, let's take a look at the pseudo-code of the SED processing text and learn about the concept of pattern space:
foreach line in file {
//放入把行Pattern_Space
Pattern_Space <= line;
// 对每个pattern space执行sed命令
Pattern_Space <= EXEC(sed_cmd, Pattern_Space);
// 如果没有指定 -n 则输出处理后的Pattern_Space
if (sed option hasn‘t "-n") {
print Pattern_Space
}
}
-
Address
-
The first one is about address, almost all of the above commands are like this (note: one of them!) indicates whether a command is executed after a successful match
[Address[,address]] [!] {cmd}
Address can be a number, or it can be a pattern, you can use a comma to separate two address represents two address of the interval, to execute command cmd, pseudo-code as follows:
bool bexec = false
foreach line in file {
if ( match(address1) ){
bexec = true;
}
if ( bexec == true) {
EXEC(sed_cmd);
}
if ( match (address2) ) {
bexec = false;
}
}
About address you can use relative locations, such as:
# 其中的+3表示后面连续3行
$ sed ‘/dog/,+3s/^/# /g‘ pets.txt
This is my cat
my cat‘s name is betty
# This is my dog
# my dog‘s name is frank
# This is my fish
# my fish‘s name is george
This is my goat
my goat‘s name is adam
-
Command packaging
-
cmd can be multiple, they can be separated by semicolons, and can be enclosed in curly braces as nested commands. Here are a few examples:
$ cat pets.txt
This is my cat
my cat‘s name is betty
This is my dog
my dog‘s name is frank
This is my fish
my fish‘s name is george
This is my goat
my goat‘s name is adam
# 对3行到第6行,执行命令/This/d
$ sed ‘3,6 {/This/d}‘ pets.txt
This is my cat
my cat‘s name is betty
my dog‘s name is frank
my fish‘s name is george
This is my goat
my goat‘s name is adam
# 对3行到第6行,匹配/This/成功后,再匹配/fish/,成功后执行d命令
$ sed ‘3,6 {/This/{/fish/d}}‘ pets.txt
This is my cat
my cat‘s name is betty
This is my dog
my dog‘s name is frank
my fish‘s name is george
This is my goat
my goat‘s name is adam
# 从第一行到最后一行,如果匹配到This,则删除之;如果前面有空格,则去除空格
$ sed ‘1,${/This/d;s/^ *//g}‘ pets.txt
my cat‘s name is betty
my dog‘s name is frank
my fish‘s name is george
my goat‘s name is adam
-
Hold Space
-
Next, we need to understand the concept of hold space, let's take a look at four commands:
G: Copy contents of hold space to pattern space, the contents of the original pattern space are cleared
G: After append the contents of hold space to the pattern space\n
H: Copy the contents of the pattern space into hold space, and the contents of the original keep space are erased.
H: Append the contents of the pattern space to the hold space\n
X: Exchanging the contents of pattern space and hold space
What is the use of these commands? Let's take a look at two examples, and the sample files used are:
$ cat t.txt
one
two
three
A first example:
$ sed ‘H;g‘ t.txt
one
one
two
one
two
three
is not a bit not understand, I make a diagram you can understand.
The second example, reverse the line of a file:
$ sed ‘1!G;h;$!d‘ t.txt
three
two
one
One of ' 1! G;h;$!d ' can be disassembled as three commands
1! g--only the first line does not execute the G command, append the contents of hold space back to the pattern space
h--the first line executes the H command to copy the contents of the pattern space into hold space
$!d--except the last line does not execute the D command, the other rows execute the d command, delete the forward
Attach your own understanding: SED has P-zone (Pattern space) and H-zone (hold space), each read a row will put the content into the P-zone, then if we want to do something about the content before, we need to use the H area for staging some data.
Take the last diagram above, read the first line, read one to P area, and then H command to the H zone;
After reading to the P-area, then append to the H-zone, delete the P-zone in order not to output the content;
Read the last line, append the data to the H-zone, then replace the P-zone with the contents of the H-zone, and finally output the content of P-zone.
An essay on the Pattern space and hold space in SED