在GNU/Linux中,awk(gawk)和sed可用於處理文本數據。本文討論的是如何使用二者提取匹配數據行的前一行或後一行數據。如果匹配數據行出現多次,則只輸出第一次匹配到的數據行。
本文採用真實案例進行討論
Production Examples
從過軟件的官方網站提取所需數據,使用curl
或wget
下載官網HTML代碼,分別使用awk
和sed
從返回的HTML標籤數據中提取所需數據。
Previous Line For Nginx
提取匹配行的前一行,以Nginx爲例,提取最新主線(mainline)版的版本號。版本號在匹配字符串mainline
所在行的前一行,最新穩定版版本爲1.15.12
(Apr 16, 2019)。
操作命令爲
1
2
3
4
5
6
|
# Via awk
wget -qO- https://nginx.org/ | awk '$0~/mainline/{print gensub(/.*nginx-(.*)<.*/,"\\1","g",a);exit};{a=$0}'
# Via sed
wget -qO- https://nginx.org/ | sed -n -r '/mainline/{0,/mainline/{x;[email protected]*nginx-(.*)<.*@\[email protected];}};h'
# wget -qO- https://nginx.org/ | sed -n -r '0,/mainline/{/mainline/{x;[email protected]*nginx-(.*)<.*@\[email protected];p};h}'
|
演示過程
1
2
3
4
5
6
7
8
9
|
# Via awk
$ wget -qO- https://nginx.org/ | awk '$0~/mainline version/{print gensub(/.*nginx-(.*)<.*/,"\\1","g",a);exit};{a=$0}'
1.15.12
$
# Via sed
$ wget -qO- https://nginx.org/ | sed -n -r '/mainline version/{0,/mainline/{x;[email protected]*nginx-(.*)<.*@\[email protected];}};h'
1.15.12
$
|
HTML標籤片段如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<table class="news">
<tr><td class="date"><a name="2019-04-23"></a>2019-04-23</td><td><p><a href="en/download.html">nginx-1.16.0</a>
stable version has been released,
incorporating new features and bug fixes from the 1.15.x mainline branch -
including UDP proxying improvements in the
<a href="en/docs/stream/ngx_stream_core_module.html">stream module</a>,
<a href="en/docs/http/ngx_http_upstream_module.html#random">random load
balancing method</a>,
support for
<a href="en/docs/http/ngx_http_ssl_module.html#ssl_early_data">TLS 1.3
early data</a>,
dynamic loading of
<a href="en/docs/http/ngx_http_ssl_module.html#ssl_certificate">SSL
certificates</a>,
and more.
</p></td></tr><tr><td class="date"><a name="2019-04-16"></a>2019-04-16</td><td><p><a href="en/docs/njs/index.html">njs-0.3.1</a>
version has been released, featuring ES6 arrow functions support
and <a href="en/docs/njs/changes.html#njs0.3.1">more</a>.
</p></td></tr><tr><td class="date"><a name="2019-04-16"></a>2019-04-16</td><td><p><a href="en/download.html">nginx-1.15.12</a>
mainline version has been released.
</p></td></tr><tr><td class="date"><a name="2019-04-09"></a>2019-04-09</td><td><p><a href="en/download.html">nginx-1.15.11</a>
mainline version has been released.
</p></td></tr><tr><td class="date"><a name="2019-03-26"></a>2019-03-26</td><td><p><a href="en/docs/njs/index.html">njs-0.3.0</a>
version has been released, featuring ES6 modules support
and <a href="en/docs/njs/changes.html#njs0.3.0">more</a>.
</p></td></tr><tr><td class="date"><a name="2019-03-26"></a>2019-03-26</td><td><p><a href="en/download.html">nginx-1.15.10</a>
mainline version has been released.
</p></td></tr><tr><td class="date"><a name="2019-03-01"></a>2019-03-01</td><td><p><a href="https://unit.nginx.org/">unit-1.8.0</a>
version has been
<a href="http://mailman.nginx.org/pipermail/unit/2019-March/000118.html">released</a>,
featuring
<a href="https://unit.nginx.org/configuration/#routes">internal request
routing</a>
and experimental
<a href="https://unit.nginx.org/configuration/#java-application">Java Servlet
Containers</a> support.
</p></td></tr><tr><td class="date"><a name="2019-02-26"></a>2019-02-26</td><td><p><a href="en/download.html">nginx-1.15.9</a>
mainline version has been released.
</p></td></tr><tr><td class="date"><a name="2019-02-26"></a>2019-02-26</td><td><p><a href="en/docs/njs/index.html">njs-0.2.8</a>
version has been released, featuring support for setting nginx variables
and <a href="en/docs/njs/changes.html#njs0.2.8">more</a>.
</p></td></tr><tr><td class="date"><a name="2019-02-07"></a>2019-02-07</td><td><p><a href="https://unit.nginx.org/">unit-1.7.1</a>
version has been <a href="http://mailman.nginx.org/pipermail/unit/2019-February/000112.html">released</a>,
with a vulnerability fix in the router process (CVE-2019-7401).
</p></td></tr>
</table>
|
Next Line For Linux Kernel
提取匹配行的後一行,以Linux Kernel爲例,提取最新穩定(stable)版的版本號。版本號在匹配字符串stable:
所在行的後一行,最新穩定版版本爲5.0.10
(Apr 27, 2019)。
操作命令爲
1
2
3
4
5
6
|
# Via awk
wget -qO- https://www.kernel.org | awk 'match($1,/stable:/){getline;print gensub(/.*strong>(.*)<\/strong.*/,"\\1","g",$0);exit}'
# Via sed
wget -qO- https://www.kernel.org | sed -n -r '/stable:/{0,/stable:/{n;[email protected] @@g;[email protected]<[^>]*>@@gp}}'
# wget -qO- https://www.kernel.org | sed -n -r '/stable:/{0,/stable:/{n;[email protected] @@g;[email protected]<[^>]*>@@gp}}'
|
演示過程
1
2
3
4
5
6
7
8
9
10
11
12
|
# Via awk
$ wget -qO- https://www.kernel.org | awk 'match($1,/stable:/){getline;print gensub(/.*strong>(.*)<\/strong.*/,"\\1","g",$0);exit}'
5.0.10
# Via sed
$ wget -qO- https://www.kernel.org | sed -n -r '/stable:/{0,/stable:/{n;[email protected] @@g;[email protected]<[^>]*>@@gp}}'
5.0.10
# longterm有多個版本
$ wget -qO- https://www.kernel.org | sed -n -r '/longterm:/{0,/longterm:/{n;[email protected] @@g;[email protected]<[^>]*>@@gp}}'
4.19.37
$
|
HTML標籤片段如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
<table id="releases">
<tr align="left">
<td>mainline:</td>
<td><strong>5.1-rc6</strong></td>
<td>2019-04-21</td>
<td>[<a href="https://git.kernel.org/torvalds/t/linux-5.1-rc6.tar.gz" title="Download complete tarball">tarball</a>] </td>
<td> </td>
<td>[<a href="https://git.kernel.org/torvalds/p/v5.1-rc6/v5.0" title="Download patch to previous mainline">patch</a>] </td>
<td>[<a href="https://git.kernel.org/torvalds/p/v5.1-rc6/v5.1-rc5" title="Download incremental patch">inc. patch</a>] </td>
<td>[<a href="https://git.kernel.org/torvalds/ds/v5.1-rc6/v5.1-rc5" title="View diff in cgit">view diff</a>] </td>
<td>[<a href="https://git.kernel.org/torvalds/h/v5.1-rc6" title="Browse the git tree using cgit">browse</a>] </td>
<td> </td>
</tr>
<tr align="left">
<td>stable:</td>
<td><strong>5.0.10</strong></td>
<td>2019-04-27</td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.10.tar.xz" title="Download complete tarball">tarball</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.10.tar.sign" title="Download PGP verification signature">pgp</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v5.x/patch-5.0.10.xz" title="Download patch to previous mainline">patch</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v5.x/incr/patch-5.0.9-10.xz" title="Download incremental patch">inc. patch</a>] </td>
<td>[<a href="https://git.kernel.org/stable/ds/v5.0.10/v5.0.9" title="View diff in cgit">view diff</a>] </td>
<td>[<a href="https://git.kernel.org/stable/h/v5.0.10" title="Browse the git tree using cgit">browse</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v5.x/ChangeLog-5.0.10" title="View detailed change logs">changelog</a>] </td>
</tr>
<tr align="left">
<td>longterm:</td>
<td><strong>4.19.37</strong></td>
<td>2019-04-27</td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.19.37.tar.xz" title="Download complete tarball">tarball</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.19.37.tar.sign" title="Download PGP verification signature">pgp</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/patch-4.19.37.xz" title="Download patch to previous mainline">patch</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/incr/patch-4.19.36-37.xz" title="Download incremental patch">inc. patch</a>] </td>
<td>[<a href="https://git.kernel.org/stable/ds/v4.19.37/v4.19.36" title="View diff in cgit">view diff</a>] </td>
<td>[<a href="https://git.kernel.org/stable/h/v4.19.37" title="Browse the git tree using cgit">browse</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/ChangeLog-4.19.37" title="View detailed change logs">changelog</a>] </td>
</tr>
<tr align="left">
<td>longterm:</td>
<td><strong>4.14.114</strong></td>
<td>2019-04-27</td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.14.114.tar.xz" title="Download complete tarball">tarball</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.14.114.tar.sign" title="Download PGP verification signature">pgp</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/patch-4.14.114.xz" title="Download patch to previous mainline">patch</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/incr/patch-4.14.113-114.xz" title="Download incremental patch">inc. patch</a>] </td>
<td>[<a href="https://git.kernel.org/stable/ds/v4.14.114/v4.14.113" title="View diff in cgit">view diff</a>] </td>
<td>[<a href="https://git.kernel.org/stable/h/v4.14.114" title="Browse the git tree using cgit">browse</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/ChangeLog-4.14.114" title="View detailed change logs">changelog</a>] </td>
</tr>
<tr align="left">
<td>longterm:</td>
<td><strong>4.9.171</strong></td>
<td>2019-04-27</td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.9.171.tar.xz" title="Download complete tarball">tarball</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.9.171.tar.sign" title="Download PGP verification signature">pgp</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/patch-4.9.171.xz" title="Download patch to previous mainline">patch</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/incr/patch-4.9.170-171.xz" title="Download incremental patch">inc. patch</a>] </td>
<td>[<a href="https://git.kernel.org/stable/ds/v4.9.171/v4.9.170" title="View diff in cgit">view diff</a>] </td>
<td>[<a href="https://git.kernel.org/stable/h/v4.9.171" title="Browse the git tree using cgit">browse</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/ChangeLog-4.9.171" title="View detailed change logs">changelog</a>] </td>
</tr>
<tr align="left">
<td>longterm:</td>
<td><strong>4.4.179</strong></td>
<td>2019-04-27</td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.4.179.tar.xz" title="Download complete tarball">tarball</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.4.179.tar.sign" title="Download PGP verification signature">pgp</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/patch-4.4.179.xz" title="Download patch to previous mainline">patch</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/incr/patch-4.4.178-179.xz" title="Download incremental patch">inc. patch</a>] </td>
<td>[<a href="https://git.kernel.org/stable/ds/v4.4.179/v4.4.178" title="View diff in cgit">view diff</a>] </td>
<td>[<a href="https://git.kernel.org/stable/h/v4.4.179" title="Browse the git tree using cgit">browse</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v4.x/ChangeLog-4.4.179" title="View detailed change logs">changelog</a>] </td>
</tr>
<tr align="left">
<td>longterm:</td>
<td><strong>3.18.139 <span class="eolkernel" title="This release is End-of-Life">[EOL]</span></strong></td>
<td>2019-04-27</td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/linux-3.18.139.tar.xz" title="Download complete tarball">tarball</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/linux-3.18.139.tar.sign" title="Download PGP verification signature">pgp</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/patch-3.18.139.xz" title="Download patch to previous mainline">patch</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/incr/patch-3.18.138-139.xz" title="Download incremental patch">inc. patch</a>] </td>
<td>[<a href="https://git.kernel.org/stable/ds/v3.18.139/v3.18.138" title="View diff in cgit">view diff</a>] </td>
<td>[<a href="https://git.kernel.org/stable/h/v3.18.139" title="Browse the git tree using cgit">browse</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/ChangeLog-3.18.139" title="View detailed change logs">changelog</a>] </td>
</tr>
<tr align="left">
<td>longterm:</td>
<td><strong>3.16.65</strong></td>
<td>2019-04-04</td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/linux-3.16.65.tar.xz" title="Download complete tarball">tarball</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/linux-3.16.65.tar.sign" title="Download PGP verification signature">pgp</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/patch-3.16.65.xz" title="Download patch to previous mainline">patch</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/incr/patch-3.16.64-65.xz" title="Download incremental patch">inc. patch</a>] </td>
<td>[<a href="https://git.kernel.org/stable/ds/v3.16.65/v3.16.64" title="View diff in cgit">view diff</a>] </td>
<td>[<a href="https://git.kernel.org/stable/h/v3.16.65" title="Browse the git tree using cgit">browse</a>] </td>
<td>[<a href="https://cdn.kernel.org/pub/linux/kernel/v3.x/ChangeLog-3.16.65" title="View detailed change logs">changelog</a>] </td>
</tr>
<tr align="left">
<td>linux-next:</td>
<td><strong>next-20190426</strong></td>
<td>2019-04-26</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>[<a href="https://git.kernel.org/next/linux-next/h/next-20190426" title="Browse the git tree using cgit">browse</a>] </td>
<td> </td>
</tr>
</table>
|
以Nginx爲例,以字符串mainline
爲匹配關鍵詞,提取匹配行的前一行數據。
Via awk
awk的操作命令爲
1
|
wget -qO- https://nginx.org/ | awk '$0~/mainline version/{print gensub(/.*nginx-(.*)<.*/,"\\1","g",a);exit};{a=$0}'
|
解釋
wget -qO- https://nginx.org/
:獲取Nginx官網的HTML標籤;
awk ''
:表示使用awk進行操作;
$0~/mainline/
:$0
表示從文本中讀取的一整行數據,~
表示模糊匹配,/mainline/
表示匹配含有關鍵詞mainline
的數據行;
{print gensub(/.*nginx-(.*)<.*/,"\\1","g",a);exit};{a=$0}
:exit
表示只匹配一次就退出awk操作,{a=$0}
和gensub()
中的最後一個a
表示匹配行的前一行數據; 暫不理解其實現原理
gensub(/.*nginx-(.*)<.*/,"\\1","g",a)
提取具體的版本號;
print
爲awk打印命令,輸出指定的數據;
Via sed
sed的操作命令爲
使用到的選項
x
- Exchange the contents of the hold and pattern spaces.
h
- (hold) Replace the contents of the hold space with the contents of the pattern space.
p
- Print the pattern space.
-n, --quiet, --silent
- suppress automatic printing of pattern space
解釋
wget -qO- https://nginx.org/
:獲取Nginx官網的HTML標籤;
sed ''
:使用sed命令進行操作;
/mainline/
:地址定界,此處使用正則(regular expression)進行匹配,匹配關鍵爲mainline
;
/mainline/{x}
:x
表示將當前hold space和pattern space中的內容進行互換,此處只針對匹配行;
h
:將當前hold space中的內容替換爲pattern space中的內容,實現hold space中的內容與pattern space中的內容一致;
-r
:使用增強性正則表達式,如支持後向引用(back references);
-n
:表示不輸出當前pattern space中的內容,通常與p
組合使用;
/mainline/{x;p}
中的p
與-n
組合使用,表示只輸出匹配行;
0,/mainline/
:只針對第一次出現的匹配數據行;
s///g
: 替換操作,因使用了-r
,此處通過後向引用提取版本號;
完整的解釋:使用sed對獲取的HTML標籤進行處理,對於匹配數據行,先使用x
交換當前hold space和pattern space中的內容,再使用p
和-n
輸出當前pattern space中的內容;對所有數據行使用h
,將所有數據行的當前hold space中的內容替換爲pattern space中的內容。經過此操作,匹配數據行的當前pattern space中的內容即前一行數據。
稍後對其處理過程進行演示。
以Linux Kernel爲例,以字符串stable:
爲匹配關鍵詞,提取匹配行的後一行數據。
Via awk
awk的操作命令爲
1
|
wget -qO- https://www.kernel.org | awk 'match($1,/stable:/){getline;print gensub(/.*strong>(.*)<\/strong.*/,"\\1","g",$0);exit}'
|
解釋
wget -qO- https://www.kernel.org
:獲取Linux Kernel官網的HTML標籤;
awk ''
:表示使用awk進行操作;
$0
、$1
:awk中默認以空格爲字段(field)分隔符,$0
表示一整行數據,$1
表示數據行中的第一個字段(以空格爲分割符);
match($1,/stable:/)
:數據行第一個字段$1
中含有字符串stable:
,與$1~/stable:/
等效;
getline
:表示獲取匹配行的後一行數據,與exit
組合使用表示只匹配一次就結束awk操作。
print gensub(/.*strong>(.*)<\/strong.*/,"\\1","g",$0)
:根據HTML標籤格式提取並輸出版本號;
即提取匹配數據行的後一行數據,可通過awk的getline
實現。
Via sed
sed的操作命令爲
使用到的選項
n
- (next) If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed exits without processing any more commands.
解釋
wget -qO- https://www.kernel.org
:獲取Linux Kernel官網的HTML標籤;
sed ''
:使用sed命令進行操作;
/stable:/
:地址定界,此處使用正則(regular expression)進行匹配,匹配關鍵爲stable:
;
0,/stable/
:只針對第一次出現的匹配數據行;
n
: 表示將匹配行的當前pattern space中的內容,使用下一行的輸入進行替換;
-r
:使用增強性正則表達式,如支持後向引用(back references);
s///g
: 替換操作,因使用了-r
,此處通過後向引用提取版本號;
Analysis
測試數據 /tmp/test.txt
1
2
3
4
5
6
7
8
9
10
|
m1
mainline version
s1
stable flag
s2
stable version
s3
stable test
m2
mainline version
|
testing x
執行
1
|
sed '/stable/{x}' /tmp/test.txt
|
輸出爲
1
2
3
4
5
6
7
8
9
10
|
m1
mainline version
s1
s2
stable flag
s3
stable version
m2
mainline version
|
分析
line |
origin |
hold space |
pattern space |
explanation |
1 |
m1 |
|
m1 |
|
2 |
mainline version |
|
mainline version |
|
3 |
s1 |
|
s1 |
|
4 |
stable flag |
stable flag |
|
匹配行,前一行hold space爲空,交換後pattern space爲空 |
5 |
s2 |
stable flag |
s2 |
|
6 |
stable version |
stable version |
stable flag |
匹配行,前一行hold space爲stable flag ,交換後pattern space爲stable flag |
7 |
s3 |
stable version |
s3 |
|
8 |
stable test |
stable test |
stable version |
匹配行,前一行hold space爲stable version ,交換後pattern space爲stable version |
9 |
m2 |
stable test |
m2 |
|
10 |
mainline version |
stable test |
mainline version |
|
testing x&h
執行
1
|
sed '/stable/{x};h' /tmp/test.txt
|
輸出爲
1
2
3
4
5
6
7
8
9
10
|
m1
mainline version
s1
s1
s2
s2
s3
s3
m2
mainline version
|
分析
line |
origin |
hold space |
pattern space |
explanation |
1 |
m1 |
m1 |
m1 |
非匹配行,將hold space中內容替換爲pattern space中內容 |
2 |
mainline version |
mainline version |
mainline version |
|
3 |
s1 |
s1 |
s1 |
|
4 |
stable flag |
s1 |
s1 |
匹配行,前一行hold space爲s1 ,交換後pattern space爲s1 ;因爲h ,當前hold space被替換爲s1 |
5 |
s2 |
s2 |
s2 |
|
6 |
stable version |
s2 |
s2 |
匹配行,前一行hold space爲s2 ,交換後pattern space爲s2 ;因爲h ,當前hold space被替換爲s2 |
7 |
s3 |
s3 |
s3 |
|
8 |
stable test |
s3 |
s3 |
匹配行,前一行hold space爲s3 ,交換後pattern space爲s3 ;因爲h ,當前hold space被替換爲s3 |
9 |
m2 |
m2 |
m2 |
|
10 |
mainline version |
mainline version |
mainline version |
|
testing x&p&h
執行
1
|
sed -n '/stable/{x;p};h' /tmp/test.txt
|
輸出爲
分析
line |
origin |
hold space |
pattern space |
explanation |
1 |
m1 |
m1 |
m1 |
非匹配行不輸出,將hold space中內容替換爲pattern space中內容 |
2 |
mainline version |
mainline version |
mainline version |
|
3 |
s1 |
s1 |
s1 |
|
4 |
stable flag |
s1 |
s1(輸出) |
匹配行,前一行hold space爲s1 ,交換後pattern space爲s1 ;因爲h ,當前hold space被替換爲s1 |
5 |
s2 |
s2 |
s2 |
|
6 |
stable version |
s2 |
s2(輸出) |
匹配行,前一行hold space爲s2 ,交換後pattern space爲s2 ;因爲h ,當前hold space被替換爲s2 |
7 |
s3 |
s3 |
s3 |
|
8 |
stable test |
s3 |
s3(輸出) |
匹配行,前一行hold space爲s3 ,交換後pattern space爲s3 ;因爲h ,當前hold space被替換爲s3 |
9 |
m2 |
m2 |
m2 |
|
10 |
mainline version |
mainline version |
mainline version |
|
List Release News
Nginx
使用如下命令獲取Release信息,以列表形式顯示
1
2
3
|
wget -qO- https://nginx.org/ | awk '$0~/^(stable|mainline)/{$0~/stable/?type="stable":type="mainline";b=gensub(/[[:space:]]*<[^>]*>/,"","g",a);c=gensub(/nginx-/," ","g",b);printf("%s %s\n",c,type)};{a=$0}'
wget -qO- https://nginx.org/ | awk 'BEGIN{print "Date|Version|Type\n---|---|---"}$0~/^(stable|mainline)/{$0~/stable/?type="stable":type="mainline";b=gensub(/[[:space:]]*<[^>]*>/,"","g",a);c=gensub(/nginx-/,"|","g",b);printf("%s|%s\n",c,type)};{a=$0}'
|
演示過程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
$ wget -qO- https://nginx.org/ | awk '$0~/^(stable|mainline)/{$0~/stable/?type="stable":type="mainline";b=gensub(/[[:space:]]*<[^>]*>/,"","g",a);c=gensub(/nginx-/," ","g",b);printf("%s %s\n",c,type)};{a=$0}'
2019-04-23 1.16.0 stable
2019-04-16 1.15.12 mainline
2019-04-09 1.15.11 mainline
2019-03-26 1.15.10 mainline
2019-02-26 1.15.9 mainline
$ wget -qO- https://nginx.org/ | awk 'BEGIN{print "Date|Version|Type\n---|---|---"}$0~/^(stable|mainline)/{$0~/stable/?type="stable":type="mainline";b=gensub(/[[:space:]]*<[^>]*>/,"","g",a);c=gensub(/nginx-/,"|","g",b);printf("%s|%s\n",c,type)};{a=$0}'
Date|Version|Type
---|---|---
2019-04-23|1.16.0|stable
2019-04-16|1.15.12|mainline
2019-04-09|1.15.11|mainline
2019-03-26|1.15.10|mainline
2019-02-26|1.15.9|mainline
$
|
Markdown渲染如下
Date |
Version |
Type |
2019-04-23 |
1.16.0 |
stable |
2019-04-16 |
1.15.12 |
mainline |
2019-04-09 |
1.15.11 |
mainline |
2019-03-26 |
1.15.10 |
mainline |
2019-02-26 |
1.15.9 |
mainline |
Linux Kernel
使用如下命令獲取Release信息,以列表形式顯示
操作過程
Markdown渲染如下
Type |
Version |
Date |
mainline |
5.1-rc6 |
2019-04-21 |
stable |
5.0.10 |
2019-04-27 |
longterm |
4.19.37 |
2019-04-27 |
longterm |
4.14.114 |
2019-04-27 |
longterm |
4.9.171 |
2019-04-27 |
longterm |
4.4.179 |
2019-04-27 |
longterm |
3.18.139[EOL] |
2019-04-27 |
longterm |
3.16.65 |
2019-04-04 |
linux-next |
next-20190426 |
2019-04-26 |
Further Reading
Change Logs
- 2017.03.15 14:54 Wed Asia/Shanghai
- 2017.05.31 11:26 Wed Asia/Shanghai
- 添加
List Release News
,用Markdown形式輸出
- 2018.04.11 11:09 Wed America/Boston
- 2019.04.28 15:22 Sun America/Boston