[英]How do I use sed to replace all lines between two matching patterns (on OSX BSD)
[英]Replace all newlines between “two lines with certain patterns” with a comma in vim
如果必須將模式1和模式2之間的所有行都用逗號替換換行符,該怎么辦?
從:
Pattern 1
abcd
edfgads asd
adsad
...
Pattern 2
至:
Pattern 1, abcd, edfgads asd, adsad, ..., Pattern 2
使用Pattern 1
和Pattern 2
作為地址,請參見:help cmdline-ranges
:
:/^Pattern 1/,/^Pattern 2/-1 s/\n/, /
您可以在替換時輸入行號。
:{pattern1LineNo},{pattern2LineNo}s/\n/, /g
怎么樣
:%s/Pattern 1\_.\{-}Pattern 2/\=join(split(submatch(0), "\n"), ", ")/g
搜索
Pattern 1 # obvious
\_. # any character including newline
\{-} # repeat non-greedily (vim's way of writing *?)
Pattern 2 # obvious
替換部分應清晰無須說明。
:g/Pattern1/norm V/Pattern2^MgJ
:g/
在與Pattern1匹配的行上,運行normal
模式的按鍵:
v
isual盡可能選擇... /
為模式2搜索 gJ
連接選定的行,不添加空格 注意 使用Ctrl-V <Enter>
或Ctrl-Q <Enter>
鍵入^M
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.