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

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

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
  • Attain Bursaries and Cash Rewards ci421269.tw1.ru SZ
  • A transaction is awaiting completion ci421269.tw1.ru Yq
  • Prepare for a large cash disbursement co093912.tw1.ru 72
  • Like and receive a cash incentive co093912.tw1.ru 4j
  • Your VIP pass includes cash access ci421269.tw1.ru ne
  • Receive Your Cash Bonus Immediately ci421269.tw1.ru Ds
  • Take Home Your Dollar Reward ci421269.tw1.ru 9h
  • Obtain Benefits from Your Energy co093912.tw1.ru ds
  • You still need to accept your cash present co093912.tw1.ru sp
  • This cash giveaway lasts for one day only ci421269.tw1.ru DN
  • Play for a chance at quick cash yugfwegyfg.temp.swtest.ru A6
  • An exclusive monetary offer for you yugfwegyfg.temp.swtest.ru 5k
  • Your Cash Bonus is Locked   Unlock It Now ci421269.tw1.ru 7l
  • Gain Access to Tokenized Incentives co093912.tw1.ru 8t
  • Win Perks Through Consistent Activity co093912.tw1.ru lw
  • Free money is being distributed Claim yours ci421269.tw1.ru UP
  • Your hidden monetary reward awaits discovery co093912.tw1.ru iT
  • Discover your anniversary surprise yugfwegyfg.temp.swtest.ru Ov
  • You are the chosen recipient of funds co093912.tw1.ru PU
  • Secure Quick Extra Funds ci421269.tw1.ru aF
  • Become an instant cash winner co093912.tw1.ru vm
  • Join our 24 hour cash hand out event ci421269.tw1.ru Ud
  • Get paid for every friend you refer yugfwegyfg.temp.swtest.ru 3U
  • Within lies a revelation about money co093912.tw1.ru gj
未经允许不得转载: LIFE & SHARE - 王颜公子 » sed - 文本分析与转换工具 (2) 进阶

分享

作者

作者头像

Madman

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

0 条评论

暂时还没有评论.

专题系列