首页 新闻 会员 周边 捐助

在Vim中处理文本

0
悬赏园豆:15 [已解决问题] 解决于 2011-10-07 12:05

怎样可以较快的将下面的文件处理,得到结果:docm、docx、dotm、dotx、potm、potx、ppam、ppsm、ppsx、pptm、pptx、xlam、xlsb、xlsm、xlsx、xltm、xltx、

".docm","application/vnd.ms-word.document.macroEnabled.12"
".docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document"
".dotm","application/vnd.ms-word.template.macroEnabled.12"
".dotx","application/vnd.openxmlformats-officedocument.wordprocessingml.template"
".potm","application/vnd.ms-powerpoint.template.macroEnabled.12"
".potx","application/vnd.openxmlformats-officedocument.presentationml.template"
".ppam","application/vnd.ms-powerpoint.addin.macroEnabled.12"
".ppsm","application/vnd.ms-powerpoint.slideshow.macroEnabled.12"
".ppsx","application/vnd.openxmlformats-officedocument.presentationml.slideshow"
".pptm","application/vnd.ms-powerpoint.presentation.macroEnabled.12"
".pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation"
".xlam","application/vnd.ms-excel.addin.macroEnabled.12"
".xlsb","application/vnd.ms-excel.sheet.binary.macroEnabled.12"
".xlsm","application vnd.ms-excel.sheet.macroEnabled.12"
".xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
".xltm","application/vnd.ms-excel.template.macroEnabled.12"
".xltx","application/vnd.openxmlformats-officedocument.spreadsheetml.template"

风过浪静的主页 风过浪静 | 初学一级 | 园豆:68
提问于:2011-09-23 19:24
< >
分享
最佳答案
1

用perl不行么?

use strict ;
my $str ='
".docm","application/vnd.ms-word.document.macroEnabled.12"
".docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document"
".dotm","application/vnd.ms-word.template.macroEnabled.12"
".dotx","application/vnd.openxmlformats-officedocument.wordprocessingml.template"
".potm","application/vnd.ms-powerpoint.template.macroEnabled.12"
".potx","application/vnd.openxmlformats-officedocument.presentationml.template"
".ppam","application/vnd.ms-powerpoint.addin.macroEnabled.12"
".ppsm","application/vnd.ms-powerpoint.slideshow.macroEnabled.12"
".ppsx","application/vnd.openxmlformats-officedocument.presentationml.slideshow"
".pptm","application/vnd.ms-powerpoint.presentation.macroEnabled.12"
".pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation"
".xlam","application/vnd.ms-excel.addin.macroEnabled.12"
".xlsb","application/vnd.ms-excel.sheet.binary.macroEnabled.12"
".xlsm","application vnd.ms-excel.sheet.macroEnabled.12"
".xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
".xltm","application/vnd.ms-excel.template.macroEnabled.12"
".xltx","application/vnd.openxmlformats-officedocument.spreadsheetml.template"
' ;

while($str =~ m/("\.[a-z]{4}"),/g){
print $1, "\n" ;
}



收获园豆:15
翰墨小生 | 初学一级 |园豆:51 | 2011-09-24 10:01

vim的方法,

思路,首先我们要找到形如.xxxx的模式,然后将整行的内容替换为这个模式即可。

:%s/\"\.\(\a\{4}\).*/\1/g

解释一下

% 全局替换

s 替换

\" 匹配"

\. 匹配.

\(\)匹配组

\a 匹配任意英文字母

\{4}重复4次

.*匹配后面所有内容

\1第一个匹配

翰墨小生 | 园豆:51 (初学一级) | 2011-09-24 10:46

谢谢,不过最好还是把换行符也去掉,最后成这样的结果:

docm、docx、dotm、dotx、potm、potx、ppam、ppsm、ppsx、pptm、pptx、xlam、xlsb、xlsm、xlsx、xltm、xltx、

所以我修改了你的代码:

:%s/\"\.\(\a\{4}\).*\n/\1、/g

风过浪静 | 园豆:68 (初学一级) | 2011-10-07 12:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册