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

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

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
  • Get Rewarded for Diverse Unplanned Work cv039824.tw1.ru uC
  • Take possession of your USD75 today cv039824.tw1.ru w6
  • This message packs a bonus punch cv039824.tw1.ru px
  • You've reclaimed currency that had vanished cc032601.tw1.ru QT
  • Make Your Financial Reward Safe cv039824.tw1.ru 8Q
  • Sign up for the free giveaway and earn cv039824.tw1.ru 5V
  • Gain Entry to Secret Price Cuts and Payouts cc032601.tw1.ru 3Z
  • Secure your bonus before the night ends cc032601.tw1.ru yx
  • Complete tests and get cash cc032601.tw1.ru x4
  • qezouhehro
  • A mysterious monetary sum has come to light cc032601.tw1.ru Jy
  • Ensure You Receive Your Cash Reward cc032601.tw1.ru ty
  • We'll Match Your First Deposit with a Bonus cv039824.tw1.ru Cs
  • Snag your additional cash bonus now cc032601.tw1.ru dB
  • Reply and get an instant reward cv039824.tw1.ru Fh
  • Notification Unexpected funds incoming cc032601.tw1.ru YH
  • Don't leave your cash reward unclaimed cc032601.tw1.ru uo
  • A remittance awaits your approval cv039824.tw1.ru BG
  • Don't lose your loyalty funds lyuofutykc.temp.swtest.ru Bc
  • Get a cash bonus designed around you cv039824.tw1.ru 8r
  • Take Hold of Your Cash Compensation cc032601.tw1.ru zI
  • Get a Financial Perk for Signing Up lyuofutykc.temp.swtest.ru pl
  • There's a cash bonus with your name on it cc032601.tw1.ru sv
  • infvkkjqjt
未经允许不得转载: LIFE & SHARE - 王颜公子 » sed - 文本分析与转换工具 (2) 进阶

分享

作者

作者头像

Madman

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

0 条评论

暂时还没有评论.

专题系列