手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Unix文本处理工具之sed

[日期:2013-08-25] 来源:Linux社区  作者:xia7139 [字体:大 中 小]

3、初步体会sed的威力

比如你有一个文件内容如下:

test.txt:
{'books/daglib/0015113': '<title>Scale-isometric polytopal graphs in hypercubes and Z<sub>n</sub>.</title>\n',
'books/daglib/0097705': '<title>Discrete total l<sub>p</sub>-norm approximation problem for the function.</title>\n',
'books/daglib/p/AveneauCFM11': '<title>A Framework for <i>n</i>-Dimensional Visibility Computations.</title>\n',
'books/daglib/p/Carter11': '<title>Using <i>Dungeons and Dragons</i> to Integrate Curricula in Classroom.</title>\n',
'books/daglib/p/CasolaLRV11': '<title>Access Control in Cloud-on-Grid Systems: The <i>PerfCloud</i> Case Study.</title>\n',
'books/daglib/p/ChunKZDMZ11': '<title>Reverse Engineer of Gene Networks with Application <i>in silico</i> Network.</title>\n',
'books/daglib/p/ChungK11': '<title>eQTL Mapping for Functional Classes of <i>Saccharomyces cerevisiae</i> Genes wssion.</title>\n',
'books/daglib/p/Goldman11': '<title>A Model for Computer Graphics Based on Algebra for \xe2\x84\x9d<sup>3</sup>.</title>\n',
'books/daglib/p/LiZ11': '<title>Line Geometry over \xe2\x84\x9d<sup>3, 3</sup>, and Stewart Platforms.</title>\n',
'books/daglib/p/Liestol11': '<title><i>Situated Simulations</i> Between Reality and Designing a Narrative Space.</title>\n'}

现在你要将其中的各行中的类似于标签之类的东西去掉,只需用一条sed命令:

$sed -e 's/<title>//g;s/<\/title>//g' -e 's/<i>//g;s/<\/i>//g' -e 's/<sub>//g;s/<\/sub>//g' -e 's/<sup>//g;s/<\/sup>//g' test.txt
{'books/daglib/0015113': 'Scale-isometric polytopal graphs in hypercubes and Zn.\n',
'books/daglib/0097705': 'Discrete total lp-norm approximation problem for the function.\n',
'books/daglib/p/AveneauCFM11': 'A Framework for n-Dimensional Visibility Computations.\n',
'books/daglib/p/Carter11': 'Using Dungeons and Dragons to Integrate Curricula in Classroom.\n',
'books/daglib/p/CasolaLRV11': 'Access Control in Cloud-on-Grid Systems: The PerfCloud Case Study.\n',
'books/daglib/p/ChunKZDMZ11': 'Reverse Engineer of Gene Networks with Application in silico Network.\n',
'books/daglib/p/ChungK11': 'eQTL Mapping for Functional Classes of Saccharomyces cerevisiae Genes wssion.\n',
'books/daglib/p/Goldman11': 'A Model for Computer Graphics Based on Algebra for \xe2\x84\x9d3.\n',
'books/daglib/p/LiZ11': 'Line Geometry over \xe2\x84\x9d3, 3, and Stewart Platforms.\n',
'books/daglib/p/Liestol11': 'Situated Simulations Between Reality and Designing a Narrative Space.\n'}

如果要在原文件中修改,只需加-i参数。

linux
相关资讯       sed  Unix sed 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款