sed - 文本分析与转换工具 (2) 进阶

  • 原创
  • Madman
  • /
  • /
  • 0
  • 8638 次阅读

Sed - 文本分析与转换工具 (2) 进阶-min.png

Synopsis: 借助于非常好用的调试工具sedsed,可以很好地理解sed的模式空间和保持空间。在调试模式下,它会读取您的脚本并为其添加额外的命令。当执行时,你可以看到命令之间的数据流,揭示模式空间和保持空间中的缓存数据的真实情况。只有真正掌握两个缓存空间如何交换数据,才能实现sed的高级应用。同时介绍了sed如何操纵多行数据,以及改变执行的流程

sed系列:


1. 调试工具 sedsed

sedsed项目 示例脚本

1. 安装
# wget http://aurelio.net/projects/sedsed/sedsed-1.0
# chmod +x sedsed-1.0
# mv sedsed-1.0 /usr/bin/sedsed

2. 在调试模式下,它会读取您的脚本并为其添加额外的命令。当执行时,你可以看到命令之间的数据流,揭示模式空间和保持空间中的缓存数据的真实情况
# cat test.txt  测试文件原内容
The Linux
System and Linux
...

# cat test.txt | sedsed -d '/Linux$/ {N ; s/\nSystem/ Operating &/ ; P ; D}'
PATT:The Linux$
HOLD:$
COMM:/Linux$/ {
COMM:N
PATT:The Linux\nSystem and Linux$
HOLD:$
COMM:s/\nSystem/ Operating &/
PATT:The Linux Operating \nSystem and Linux$
HOLD:$
COMM:P
The Linux Operating 
PATT:The Linux Operating \nSystem and Linux$
HOLD:$
COMM:D
PATT:System and Linux$
HOLD:$
COMM:/Linux$/ {
COMM:N
PATT:System and Linux\n...$
HOLD:$
COMM:s/\nSystem/ Operating &/
PATT:System and Linux\n...$
HOLD:$
COMM:P
System and Linux
PATT:System and Linux\n...$
HOLD:$
COMM:D
PATT:...$
HOLD:$
COMM:/Linux$/ {
PATT:...$
HOLD:$
...

3. 在缩进模式下,您的脚本被重新格式化为标准间距
# cat myscript.sed
/Linux$/{N;s/\nSystem/ Operating &/;P;D}
# sedsed --indent -f myscript.sed 
/Linux$/ {                             
    N                                  
    s/\nSystem/ Operating &/           
    P                                  
    D                                  
} 

4. 在HTMLize模式下,您的脚本会被转换为美丽的彩色HTML文件,并且所有命令和参数都会被标识以供您欣赏
# sedsed --htmlize -f myscript.sed > sedsed.html

5. 在标记模式中,您可以看到您使用的每个命令的元素
# sedsed -t '/Linux$/ {N ; s/\nSystem/ Operating &/ ; P ; D}'

2. pattern spacehold space

除了sed入门篇里面介绍的pattern space,还有一个叫hold space的缓冲区,它也是默认为空。sed在每次循环(处理完一行)后会清空pattern space,但是不会清空hold space,所以可以用hold space来临时保存一些行内容。sed命令不能直接对hold space中的内容执行命令,但是可以将内容在pattern spacehold space中交换:

  • hpattern space中的内容替换hold space中的内容
  • Hpattern space中的内容追加到hold space中(先追加一个换行符\n)
  • ghold space中的内容替换pattern space中的内容
  • Ghold space中的内容追加到pattern space中(先追加一个换行符\n)
  • x交换pattern space中的内容和hold space中的内容
1. 打印偶数行
# sed -n 'x ; n ; p' books.txt
2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones, George R. R. Martin, 864

2. 打印奇数行
# sed -n 'x ; n ; x ; p' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
3) The Alchemist, Paulo Coelho, 197 
5) The Pilgrimage, Paulo Coelho, 288

3. 打印包含Paulo的前面一行,即第2、4行
# sed -n '/Paulo/! h ; /Paulo/ {x ; p}' books.txt
2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432

4
                                
                            
分类: Linux
标签: GNU sed
  • Dive into Investing and Grab Free Cash cx180165.tw1.ru zv
  • Educate Yourself and Obtain a Refund cv505813.tw1.ru fg
  • Share a post earn dollars cv505813.tw1.ru nK
  • Your individually tailored cash bonus cv505813.tw1.ru ke
  • You've laid claim to deserted finances cv505813.tw1.ru RX
  • Today is Your Day for a Cash Bonus fdjhgkhjkg.temp.swtest.ru G1
  • Claim your located cash prize without delay cx180165.tw1.ru Df
  • Receive a brand new cash present fdjhgkhjkg.temp.swtest.ru 6N
  • Access your special cash offer now cx180165.tw1.ru lY
  • Open this for a bonus energy shockwave cv505813.tw1.ru D1
  • Come and Get Your Money Incentive cv505813.tw1.ru 23
  • Reveal a cash prize by scratching here cv505813.tw1.ru 30
  • Profit from Your Very Presence cv505813.tw1.ru x5
  • A 12 hour only exclusive for you fdjhgkhjkg.temp.swtest.ru JE
  • Reply and get an instant reward cv505813.tw1.ru sB
  • Earn a Bonus Upon Registration cx180165.tw1.ru 0i
  • Be Compensated for Sundry Duties fdjhgkhjkg.temp.swtest.ru g4
  • The bonus of the season is now available cv505813.tw1.ru 5b
  • Receive a cash reward for commenting cv505813.tw1.ru vs
  • A sleeping financial asset has been roused cx180165.tw1.ru 6S
  • Get an On the Spot Cash Win cx180165.tw1.ru 4H
  • Instantaneous money winning opportunities cv505813.tw1.ru EA
  • Don't lose your money it's expiring soon cv505813.tw1.ru nO
  • Your update comes with a cash bonus fdjhgkhjkg.temp.swtest.ru 7P
未经允许不得转载: LIFE & SHARE - 王颜公子 » sed - 文本分析与转换工具 (2) 进阶

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列