开发者

how to extract text that has already matched the multiple line pattern?

开发者 https://www.devze.com 2023-02-16 03:05 出处:网络
all using vim meta-character \\_. or awk, I have matched the multiple lines, but I don开发者_如何学C\'t know how to yank or extract them into other files.Is there a general way to do this? This answer

all using vim meta-character \_. or awk, I have matched the multiple lines, but I don开发者_如何学C't know how to yank or extract them into other files.Is there a general way to do this?


This answer is applying to Vim, not Awk.

I can suggest:

function CopyPatternToRegisterZ(pat)
  let @z .= a:pat
  return a:pat
endfunction

And then:

:let @z = ''
:%s/your_pattern/\= CopyPatternToRegisterZ(submatch(0)) /g

Then you can use "zp to paste your matches to another file.

See :help sub-replace-expression for details on this syntax.


use print in awk then redirect output to other file .

awk 'BEGIN {FS =" "};  { if ($0 ~ /(expression)/) { print $0 } }' inputfile.txt > outputfile.txt


Only to copy pattern to another file

"between marks 
:'a,'b g/^Error/ . w >> errors.txt

"entire file
:% g/pattern/ . w >> log.txt

"to display "whit numbers", lines containing the desired pattern
:g/pattern/#


If you are at the start of the search, y//e<CR> will copy the whole matched string into default register. Note that after this action n will bring you to the end of the current search (because e offset flag is saved), if you want n to continue bringing you to the start, you should additionally type //<CR> (that clears all offset flags). So, the whole key sequence is

/<pattern><CR>y//e<CR>//<CR>N
0

精彩评论

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