保持空间(hold space)
将模式空间的内容复制到保持空间,保持空间的内容也可以复制到模式空间,通过命令移动保持空间和模式空间的数据。保持空间用于临时存储,当改变模式空间中的原始内容时,用于保留当前输入行的副本。单独的命令不能寻址保持空间或者更改它的内容。(小写字母命令改写目的缓存区的内容,而大写字母命令追加缓存区的现有内容)
Hold命令在保持空间的内容之后放置一个换行符,且后面跟随模式空间的内容(即使保持空间是空的,换行符也被追加到保持空间中)。
Get命令模式空间的内容之后放置一个换行符,且后面跟随保持空间的内容。
Exchange命令交换两个缓冲区的内容,对两个缓存区没有副作用。
find the Match statement
Consult the Get statement
using the Read statement to retrieve data
/the .* statement/{
h # 以改写方式将匹配行存入hold space
# Pattern space:find the Match statement
# Hold space:find the Match statement
s/.*the \(.*\) statement.*/\1/ # 将模式空间中的匹配字符拆分保留the 与 statement之间的字符
# Pattern space:Match
# Hold space:find the Match statement
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ # 将模式空间中的保留字符小->大
# Pattern space:MATCH
# Hold space:find the Match statement
G # 将hold space中的字符追加到pattern space中
# Pattern space:MATCH\nfind the Match statement
# Hold space:find the Match statement
s/\(.*\)\n\(.*the \).*\( statement.*\)/\2\1\3/ # 重新组织字符串
# Pattern space:find the MATCH statemt
# Hold space:find the Match statement
}
result:
find the MATCH statement
Consult the GET statement
using the READ statement to retrieve data