1

有没有办法只在模式搜索中抓取和导出匹配部分而不更改当前文件?

例如,来自包含以下内容的文件:

57","0","37","","http://www.thisamericanlife.org/Radio_Episode.aspx?episode=175"
58","0","37","","http://www.thisamericanlife.org/Radio_Episode.aspx?episode=170"

我想导出一个新文件,其中包含:

http://www.thisamericanlife.org/Radio_Episode.aspx?episode=175
http://www.thisamericanlife.org/Radio_Episode.aspx?episode=170

我可以通过使用这样的替换来做到这一点:

:s/.\{-}\(http:\/\/.\{-}\)".\{-}/\1/g
:%w>>data

但是替换命令会更改当前文件。有没有办法在不更改当前文件的情况下做到这一点?

更新:

我正在寻找这样的命令:

:g/pattern/.w>>newfile

此命令写入匹配发生的整行。我只想导出匹配项,而不是整行。

4

3 回答 3

2
redir >>newfile
g/^/let g:match=matchstr(getline(line('.')), pattern) | if g:match!=#"" | silent echo g:match | endif
redir END

解释:

redir >> newfile
开始将消息重定向到 file newfile,如果存在则追加。
g/^/
对于每一行
getline(line('.'))
获取行内容
let g:match(getline(line('.')), pattern)
找到匹配的行的一部分pattern并将其保存在g:match变量中
if g:match!=#""
如果行匹配模式
echo g:match
输出匹配行
silent echo g:match
但仅限于命令指定的位置redir
redir END
停止重定向输出。
于 2010-06-13T13:02:39.357 回答
2

只需更改您的订单:

 :w newfile.txt
 :e newfile.txt
 :%s/.\{-}\(http:\/\/.\{-}\)".\{-}/\1/g
 :w
于 2010-06-13T13:03:38.997 回答
0

你是这个意思吗?

:e currfile.txt
:s/.\{-}\(http:\/\/.\{-}\)".\{-}/\1/g
:w newfile.txt

或者您想继续编辑以前的文件吗?

编辑:或者你可以简单地撤消(按'u')。

于 2010-06-13T10:07:06.100 回答