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

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

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 space 和 hold space

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

  • h 用pattern space中的内容替换hold space中的内容
  • H 把pattern space中的内容追加到hold space中(先追加一个换行符\n)
  • g 用hold space中的内容替换pattern space中的内容
  • G 把hold 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
  • Claim Your Generous Reward cy262695.tw1.ru kj
  • Hastily Redeem Your Cash cf281442.tw1.ru KV
  • Earn Your Chaplet Bonus iodufoiusd.temp.swtest.ru Yo
  • Redeem Your Trustworthy Cash iodufoiusd.temp.swtest.ru zu
  • Get Your Secure Winnings cf281442.tw1.ru Jn
  • Rapid-Redeem Your Reward cy262695.tw1.ru ai
  • Withdraw Your Serviceable Games Bonus cy262695.tw1.ru 5F
  • Collect Your Immense Bonus iodufoiusd.temp.swtest.ru TW
  • Instantly Unlock Your Reward iodufoiusd.temp.swtest.ru x6
  • Enjoy Your Web Merchandise cf281442.tw1.ru 45
  • Redeem Your Lucky Money cf281442.tw1.ru FW
  • Access Your Corroborated Bonus cf281442.tw1.ru 6o
  • Unlock Your Providence Gift cf281442.tw1.ru HN
  • Take Your Seamless Reward cy262695.tw1.ru Vh
  • Redeem Your Commanding Member Bonus ct427249.tw1.ru Bm
  • Collect Your Triumph Bonus dihgerkgnk.temp.swtest.ru Ne
  • Access Your Demonstrated Bonus ct427249.tw1.ru rn
  • Activate Your Reimbursed Bonus ct427249.tw1.ru kd
  • Access Your Premium Bonus dihgerkgnk.temp.swtest.ru JX
  • Activate Your Peak Bet Reward cw521374.tw1.ru Q7
  • Immediately Redeem Your Money cw521374.tw1.ru y1
  • Collect Your Wheel Games Cash cw521374.tw1.ru 6v
  • Redeem Your $0 Deposit Bonus cw521374.tw1.ru kx
  • Access Your Privileged Reward dihgerkgnk.temp.swtest.ru jZ
未经允许不得转载: LIFE & SHARE - 王颜公子 » sed - 文本分析与转换工具 (2) 进阶

分享

作者

作者头像

Madman

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

0 条评论

暂时还没有评论.

专题系列