$ sed -n "/00:00:01/p" nls.log
1 223.104.7.59 - - [05/Dec/2018:00:00:01 +0000] "
4 139.180.131.123 - - [05/Dec/2018:00:00:01 +0000] "
9 139.180.131.123 - - [05/Dec/2018:00:00:01 +0000] "
10 117.136.39.87 - - [05/Dec/2018:00:00:01 +0000] "
$ sed -n "/00:00:02/p" nls.log
2 123.125.71.60 - - [05/Dec/2018:00:00:02 +0000] "
3 141.8.142.131 - - [05/Dec/2018:00:00:02 +0000] "
$ sed -n "/00:00:01/,/00:00:02/p" nls.log
1 223.104.7.59 - - [05/Dec/2018:00:00:01 +0000] "
2 123.125.71.60 - - [05/Dec/2018:00:00:02 +0000] "
4 139.180.131.123 - - [05/Dec/2018:00:00:01 +0000] "
5 40.77.167.60 - - [05/Dec/2018:00:00:03 +0000] "
6 204.141.42.226 - - [05/Dec/2018:00:00:04 +0000] "
7 40.77.167.60 - - [05/Dec/2018:00:00:04 +0000] "
8 127.0.0.1 - - [05/Dec/2018:00:00:04 +0000] "
9 139.180.131.123 - - [05/Dec/2018:00:00:01 +0000] "
10 117.136.39.87 - - [05/Dec/2018:00:00:01 +0000] "
11 121.15.166.147 - - [05/Dec/2018:00:00:07 +0000] "
12 219.142.131.122 - - [05/Dec/2018:00:00:07 +0000] "
13 61.132.54.2 - - [05/Dec/2018:00:00:07 +0000] "
14 103.75.152.102 - - [05/Dec/2018:00:00:07 +0000] "
15 222.66.96.129 - - [05/Dec/2018:00:00:07 +0000] "
16 180.168.166.28 - - [05/Dec/2018:00:00:07 +0000] "
17 114.94.126.62 - - [05/Dec/2018:00:00:07 +0000] "
18 61.135.169.90 - - [05/Dec/2018:00:00:07 +0000] "
19 120.36.255.107 - - [05/Dec/2018:00:00:07 +0000] "
20 14.21.33.152 - - [05/Dec/2018:00:00:07 +0000] "
sed -n '/s1/,/s2/p' file
sed 会逐行读取文件到模式空间,也就是一个临时缓存,然后执行 sed 后面的命令,然后输出打印模式空间的内容,然后清空模式空间;
接着读文件的下一行,重复前面的步骤,直到文件内容处理完为止。
如果没有-n,匹配到的内容比如 s1 会被再次打印,s1 总共就会被打印 2 次 (假设 s1 只匹配到 1 行)。
如果有-n 那么会抑制默认打印,只打印匹配到的内容。
'/s1/,/s2/p' 表示打印 s1 和 s2 所在行及之间的内容。
所以,可以推断出 s1 和 s2 直接的内容是被存到模式空间,并且会被打印的。
那么,当 s1 匹配成功,s2 匹配失败时,打印的是 s1 及 s1 到最后 1 行。
案例中,$ sed -n "/00:00:01/,/00:00:02/p" nls.log 的/00:00:01/的所在 4/9/10 行都必/00:00:02/所在 2/3 行大,
而/00:00:01/所在行是 1,所以会打印所有。