开发者

vi, find line and output/output to separate file

开发者 https://www.devze.com 2023-01-14 09:54 出处:网络
I\'m looking for a command in VI/VIM to search for particular text in the file and grab the whole line and output in either on the screen or separate file.e.g.开发者_Go百科

I'm looking for a command in VI/VIM to search for particular text in the file and grab the whole line and output in either on the screen or separate file. e.g.开发者_Go百科

This is some line with this _word_ and some other text.
This is some line with this some other text and some other text.

so this would output only the first line..


:redir > output.txt
:g/_word_/p
:redir END

The line will be output to the screen and to output.txt. See :h :redir.

EDIT: I agree with others who suggest using plain old *nix grep if you can. Situations where you might not be able to use grep:

  • You're searching buffer text that doesn't exist in a file on the filesystem.
  • You're using Vim-specific regex extensions, like cursor location, column number, marks, etc.
  • You want do this in a cross-platform way, and grep might not exist on the system you're using.

redir can be useful in these situations.


use the g (global) command:

:g/_word_/y

will yank all lines containing _word_


having mentioned the DOS find command, you probably want to use grep:

grep -h '_word_' * > results


in vi, from command mode

search for _word_

 /_word_

yank the line

yy

paste the line

p
0

精彩评论

暂无评论...
验证码 换一张
取 消