sed系列:pattern buffer和hold buffer

As its name implies, sed hold buffer is used to save all or part of the sed pattern space for subsequent retrieval.
The contents of the pattern space can be copied to the hold space, then back again.
No operations are performed directly on the hold space. sed provides a set of hold and get functions to handle these movements.

Sed h function
The h (hold) function copies the contents of the pattern space into a holding area (also called as sed hold space), destroying any previous contents of the holding area.

Sed H function
The H function appends the contents of the pattern space to the contents of the holding area. The former and new contents are separated by a newline.

Sed g function
The g function copies the contents of the holding area into the pattern space, destroying the previous contents of the pattern space.

Sed G function
The G function appends the contents of the holding area to the contents of the pattern space. The former and new contents are separated by a newline. The maximum number of addresses is two.

Sed x function
The exchange function interchanges the contents of the pattern space and the holding area. The maximum number of addresses is two.
创建示例文件
$ cat thegeekstuff.txt
#Linux
Administration
Scripting
Tips and Tricks

#Windows
Administration

#Database
Mysql
Oracle
Queries
Procedures


例1:给文件每行之间加一个空行
$sed 'G' thegeekstuff.txt
#Linux

Administration

Scripting

Tips and Tricks

#Windows

Administration

#Database

Mysql

Oracle

Queries

Procedures

$
例2:以相反的顺序输出文件内容,以行为单位
$sed -n '1!G;h;$p' thegeekstuff.txt
Procedures
Queries
Oracle
Mysql
#Database

Administration
#Windows

Tips and Tricks
Scripting
Administration
#Linux
$
1.First line will be placed into the hold space as it is.
2.From the 2nd line onwards, just append the hold space content with the pattern space. (Remember 2nd line is in pattern space, and 1st line is in hold space).
3.Now 1st and 2nd line got reversed and move this to the hold space.
4.Repeat the above steps till last line.
5.Once the last line is reached, just append the hold space content with the pattern space and print the pattern space.
例3:模式匹配输出一段内容
$ sed -e '/./{H;$!d;}' -e 'x;/Administration/!d' thegeekstuff.txt

#Linux
Administration
Scripting
Tips and Tricks

#Windows
Administration
$
1.Till the empty line comes, keep appending the non empty lines into the hold space
2.When empty line comes i.e paragraph ends, exchange the data between pattern and hold space. So that whole paragraph will be available in pattern space.
Check if pattern “Administration” is available, if yes don’t delete it i.e print the pattern space
例4:输出模式匹配的前一行
$ sed -n '/Mysql/{g;1!p;};h' thegeekstuff.txt
#Database

For each cycle, place the line into hold buffer, if it doesn’t match with the pattern “Mysql”.
If the line matches with the pattern, get the data from the hold space(previous line) using g command and print it.
In case, if the first line matches with the pattern “Mysql”,anyway hold space will be empty.(There is no previous line to the first line).So first line should not get printed(1!p)

例5:删除每个段落的最后一行
$ sed -n -e '/^$/{x;d}' -e '/./x;p' thegeekstuff.txt

#Linux
Administration
Scripting

#Windows

#Database
Mysql
Oracle
Queries

If the line is not empty,then exchange the line between pattern and hold space. So first line will be placed in the hold space.
When next non empty line comes, exchange the pattern space and hold space, and print the pattern space. i.e first non empty line will be printed and 2nd line goes to hold. And in next cycle, 2nd non empty line is printed when 3rd line goes to hold and goes on like this.
When empty line comes (previous line to the empty line will be available in hold buffer) just exchange pattern and hold space, and delete the line (last line of the paragraph) and start the next cycle.

例6:对于每一行,把上一行的内容添加到下一行的末尾
$sed 'H;x;s/^\(.*\)\n\(.*\)/\2\1/' thegeekstuff.txt
#Linux
Administration#Linux
Scripting Administration
Tips and Tricks Scripting
Tips and Tricks
#Windows
Administration#Windows
Administration
#Database
Mysql#Database
Oracle Mysql
Queries Oracle
Procedures Queries
$
Place the first line in Hold buffer.
When the second line comes, append to Hold space (first line)
Then exchange pattern and hold buffer. So now pattern space will have first and second line separated by \n, Hold space will have only second line.
So interchange the lines in the pattern space.
The above steps happens till the end of the file

例7:把标签放到以一行内容的行头
$sed '
> /^#/{
> h
> d
> }
> G
> s/^\(.*\)\n#\(.*\)/\2 \1/' thegeekstuff.txt
Linux Administration
Linux Scripting
Linux Tips and Tricks
Linux
Windows Administration
Windows
Database Mysql
Database Oracle
Database Queries
Database Procedures
$
1.When the first line of a block is met (beginning with #)
keep that line to the Hold Space via command `h’
Then delete using ‘d’ to start another cycle.
2.For the rest lines of a block, Command `G’ appends the tag line from the
Hold Space and substitute command interchanges tag and lines properly.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值