Vim - 如何使用匹配模式连接线
Vim - how to join lines using matching pattern
我有一个包含企业联系信息的 txt 文件。目前,每一行包含业务的不同数据。我正在尝试构建一个竖线分隔的文件,在一行中包含每个企业的所有信息。问题是每个企业都有不同数量的线路。所以文件看起来像这样:
Awesome Company Inc|
Joe Smith, Owner|
Jack Smith, Manager|
Phone: (555)456-2349|
Fax: (555)456-9304|
Website: www.awesomecompanyinc.com [HYPERLINK: http://www.awesomecompanyinc.com]|
* Really Cool Company|
* Line of business: Awesomesauce|
Killer Products LLC|
Jack Black, Prop|
Phone: (555)234-4321|
Fax: (555)912-1234|
1234 Killer Street, 1st Floor|
Houston, TX 77081|
* Apparel for the classy assassin|
* Fearful Sunglasses|
* Member of the National Guild of Killers since 2001|
* Line of business: Fuhgettaboutit|
等等
所以我可以使用 :g/<pattern>/j
来连接模式中的线条,但我无法确定模式应该是什么。在上面的示例中,第 1-9 行需要连接,然后是第 10-19 行。
关键是以 2 个空格和一个星号开头的行:
* Line of business
模式基本上应该是:"Starting with the first line beginning with a letter, join all lines until the first line after the last line beginning with \ \ \*\
, then repeat until the end of the file."
我该怎么写呢?我是否应该分两步进行 - 即,有没有办法首先连接所有以字母开头的行,然后连接所有以 \ \ \*\
开头的行,然后连接每个结果对?
从以字母开头的第一行开始,连接所有行,直到以 \ \ *\
开头的最后一行之后的第一行,然后重复直到文件结尾。
实际上,您几乎可以将其直接翻译成 Vimscript:
- 以字母开头的第一行是
/^\a/
- 直到以
*
开头的最后一行之后的第一行是/^ \* .*\n\a
:找到以项目符号开头的行(^ \*
),匹配该行的其余部分 (.*
),并断言下一行不是带项目符号的行 (\n\a
)
- 然后重复直到文件结束。通过
:global
完成
合计:
:global/^\a/,/^ \* .*\n\a/join
编辑:没关系,只是意识到必须设置一堆设置才能使我的解决方案起作用。要使其正常工作,您需要
for i in range(10)
try
v/business/join
endtry
endfor
甚至假设没有一个业务块的行数超过 1024 行。那时你还不如使用 ranges
我有一个包含企业联系信息的 txt 文件。目前,每一行包含业务的不同数据。我正在尝试构建一个竖线分隔的文件,在一行中包含每个企业的所有信息。问题是每个企业都有不同数量的线路。所以文件看起来像这样:
Awesome Company Inc|
Joe Smith, Owner|
Jack Smith, Manager|
Phone: (555)456-2349|
Fax: (555)456-9304|
Website: www.awesomecompanyinc.com [HYPERLINK: http://www.awesomecompanyinc.com]|
* Really Cool Company|
* Line of business: Awesomesauce|
Killer Products LLC|
Jack Black, Prop|
Phone: (555)234-4321|
Fax: (555)912-1234|
1234 Killer Street, 1st Floor|
Houston, TX 77081|
* Apparel for the classy assassin|
* Fearful Sunglasses|
* Member of the National Guild of Killers since 2001|
* Line of business: Fuhgettaboutit|
等等
所以我可以使用 :g/<pattern>/j
来连接模式中的线条,但我无法确定模式应该是什么。在上面的示例中,第 1-9 行需要连接,然后是第 10-19 行。
关键是以 2 个空格和一个星号开头的行:
* Line of business
模式基本上应该是:"Starting with the first line beginning with a letter, join all lines until the first line after the last line beginning with \ \ \*\
, then repeat until the end of the file."
我该怎么写呢?我是否应该分两步进行 - 即,有没有办法首先连接所有以字母开头的行,然后连接所有以 \ \ \*\
开头的行,然后连接每个结果对?
从以字母开头的第一行开始,连接所有行,直到以 \ \ *\
开头的最后一行之后的第一行,然后重复直到文件结尾。
实际上,您几乎可以将其直接翻译成 Vimscript:
- 以字母开头的第一行是
/^\a/
- 直到以
*
开头的最后一行之后的第一行是/^ \* .*\n\a
:找到以项目符号开头的行(^ \*
),匹配该行的其余部分 (.*
),并断言下一行不是带项目符号的行 (\n\a
) - 然后重复直到文件结束。通过
:global
完成
合计:
:global/^\a/,/^ \* .*\n\a/join
编辑:没关系,只是意识到必须设置一堆设置才能使我的解决方案起作用。要使其正常工作,您需要
for i in range(10)
try
v/business/join
endtry
endfor
甚至假设没有一个业务块的行数超过 1024 行。那时你还不如使用 ranges