Vim:合并正则表达式匹配行

Vim:合并正则表达式匹配行

我粘贴了在 Windows 2008 服务器上通过 Cygwin 运行 rsync 生成的日志。我正在将数据同步到新的文件服务器(Synology),在删除任何不是错误的内容后,我有一些不同的错误需要处理,而且它们都是交错的。

我想要的是快速、轻松地(最好使用 vim)分离出不同的错误类型,以便合并所有以“rsync: send_files (blah blah)”开头的行,以及所有以“文件已消失:“已合并等。

基本上我试图找到一种方法来匹配正则表达式,拉出/删除所有相关行,然后将它们粘贴到同一个文件中但全部粘贴到同一个位置中,或者粘贴到新文件/缓冲区中。

编辑:我刚刚想到一个想法。如果我对这些数据进行排序,它可能会达到我想要的效果。我可以使用外部工具来做到这一点,但是是否可以在 vim 内部执行此操作,通过在 vim 中调用 sort 或更好的是,使用 vim 本机环境中的工具。事实上,我用这个作为练习来学习如何更好地使用 vim,而不是其他工具。

日志文件的一个小样本,其中一些信息经过编辑:

rsync: send_files failed to open "/cygdrive/E/Users/SharedFiles/3DS SALES/3DS MARKETING/PORTFOLIOS/2012 - Portfolios/Signage"XXXXXXXX Full Res photos/xxxxx xx sky/.DS_Store": Permission denied (13)
file has vanished: "/cygdrive/E/Users/SharedFiles/3DS SALES/CURRENT SALES/xxxxxxxxx xxx xxxxx xxxxx xxxx - xxxx/02_PROPOSAL-QUOTATION/15-002 - xxxx xxxx xxxx - Replacement  Private Fuction  Event Board.xlsx"
rsync: opendir "/cygdrive/E/Users/SharedFiles/3DS SALES/CURRENT SALES/Archived Projects/ARCHIVES -xxxxx light/Hootsuite xxxxxx Info" failed: Permission denied (13)
rsync: opendir "/cygdrive/E/Users/SharedFiles/3DS STAFF/Cortney/xxxxx/MANUAL/Support Files/xxx/Photos-Jan-2013" failed: Permission denied (13)
file has vanished: "/cygdrive/E/Users/SharedFiles/3DS STAFF/Reg/2012 - DEAD PROJECTS/2213-xxxxxx&am;C 60x114-5-HR_pdf/xxxxxx 60 x114.5 _HR.pdf"
file has vanished: "/cygdrive/E/Users/SharedFiles/3DS STAFF/Reg/2012 - DEAD PROJECTS/2213-xxxxxx&am;C 60x114-5-HR_pdf/xxxxxx 47 x94_HR.pdf"
Time value of 3DS STAFF/X-employees/xxxxxxx/Jobs- do not remove/1OLD FILES/J-M/Jade/xxxxx logo_cmyk.ai-print on paper.eps truncated on receiver.
Time value of 3DS STAFF/X-employees/xxxxxx/Jobs- do not remove/1OLD FILES/J-M/Jade/xxxx logo_rgb.ai-website.eps truncated on receiver.
file has vanished: "/cygdrive/E/Users/SharedFiles/3DS STAFF/X-employees/xxxxx/Found on iMac/xxxxxx FOLDER/Databases " Presentations " Portfolios/Databases/Database  C .xlsx"
file has vanished: "/cygdrive/E/Users/SharedFiles/CURRENT FILES/12050 - xxxxx Canada - xxxxx National Park/ARTWORK DRAWINGS/xxxxx Graphic Layouts/Batch 4 pdfs for Trish/kathy's Batch 4 pdfs for xxxxx/Links/1 =1' person .eps"
rsync: opendir "/cygdrive/E/Users/SharedFiles/CURRENT FILES/12050 - xxxxx Canada - xxxxx National Park/MANUAL/03_ xxxxx Final Manual_June-2014/ MANUAL FOR CLIENT" failed: Permission denied (13)
rsync: opendir "/cygdrive/E/Users/SharedFiles/CURRENT FILES/12050 - xxxxx Canada - xxxxx National Park/MANUAL/03_ xxxxx Final Manual_June-2014/SECTION 2.0-Exhibits" failed: Permission denied (13)
rsync: opendir "/cygdrive/E/Users/SharedFiles/CURRENT FILES/12050 - xxxxx Canada - xxxxx National Park/MANUAL/03_ xxxxx Final Manual_June-2014/SECTION 7.0_Appendices/Appendix E - Artifact Conservation and Maintenance Manual" failed: Permission denied (13)

答案1

您可以:redir为此使用:

                                                        *:重迪* *:重迪*
:redi[r][!] > {file} 将消息重定向到文件 {file}。这些消息
                        命令的输出被写入该文件,
                        直到重定向结束。留言也依旧
                        显示在屏幕上。当包含 [!] 时,
                        现有文件被覆盖。当省略 [!] 时,
                        并且 {file} 存在,该命令失败。
                        一次只能有一个“:redir”处于活动状态。致电
                        ":redir" 将关闭之前的任何活动重定向
                        开始重定向到新目标。

因此,要捕获所需的消息并将其附加到当前文件中:

:redir @+
:g/vanished/
:g/send_files/
:g/any_other_regex_you_want/
:redir END
"+P

这会将正则表达式匹配发送到剪贴板寄存器 ( @+),然后将它们粘贴到当前文档中:"+P

答案2

用于redir重定向到新文件:

:redir > rsync.log
:g/^rsync/
:redir > file_vanished.log
:g/^file/
:redir END
:q

cat rsync.log
rsync: send_files failed to open "/cygdrive/E/Users/SharedFiles/3DS SALES/3DS MARKETING/PORTFOLIOS/2012 - Portfolios/Signage"XXXXXXXX Full Res photos/xxxxx xx sky/.DS_Store": Permission denied (13)
rsync: opendir "/cygdrive/E/Users/SharedFiles/3DS SALES/CURRENT SALES/Archived Projects/ARCHIVES -xxxxx light/Hootsuite xxxxxx Info" failed: Permission denied (13)
rsync: opendir "/cygdrive/E/Users/SharedFiles/3DS STAFF/Cortney/xxxxx/MANUAL/Support Files/xxx/Photos-Jan-2013" failed: Permission denied (13)
rsync: opendir "/cygdrive/E/Users/SharedFiles/CURRENT FILES/12050 - xxxxx Canada - xxxxx National Park/MANUAL/03_ xxxxx Final Manual_June-2014/ MANUAL FOR CLIENT" failed: Permission denied (13)
rsync: opendir "/cygdrive/E/Users/SharedFiles/CURRENT FILES/12050 - xxxxx Canada - xxxxx National Park/MANUAL/03_ xxxxx Final Manual_June-2014/SECTION 2.0-Exhibits" failed: Permission denied (13)
rsync: opendir "/cygdrive/E/Users/SharedFiles/CURRENT FILES/12050 - xxxxx Canada - xxxxx National Park/MANUAL/03_ xxxxx Final Manual_June-2014/SECTION 7.0_Appendices/Appendix E - Artifact Conservation and Maintenance Manual" failed: Permission denied (13)

答案3

您可以使用内置的快速修复列表收集所有匹配的行,用:vimgrep.例如:

:vimgrep /^rsync: / %
:copen

插件替代品

我的提取匹配插件提供(除其他外):GrepToReg命令。有了它,您可以收集寄存器中的匹配行,然后将其粘贴到新的暂存缓冲区中:

:GrepToReg /^rsync:/
:new | put!

相关内容