理解 vim 中的模式

理解 vim 中的模式

我有一个以下文件:

8635147+876+5597+1686+54=8643360
2415+382376+88324+81544+926+68619475+677+222852=69398589
80+21+4478882+8945092=13424075
62+9598=9660
16832904+537+51460155+6822+2+4901436+47443+5669+855928+8113549+424282=82648727
33+9872=9905
555839
5017598+5639262+4+62+9413+4+4991+41568=10712902
4977+164+77+1018643+593851+83730=1701442
220+6831+26=7077
5+54102+1034451=1088558
37844
1
35+2983325+0+6400=2989760
8081+8361365+6+477=8369929
66+68232+9406935+6489662=15964895
6569+59336692+75+11328=59354664
28332+725+2683+45913425+9648987+4911=55599063
69724687+8+7+9940+5568+29585+518916=70288711
1804642
659157+5144361+7072+16+4799+811+58742059+451875+138174=65148324
2288508+509472+43+83704=2881727
872027+1115415+1+47922+547008+56+5550+71642773+948394=75179146
142

我想将其分类vim为:

16832904+537+51460155+6822+2+4901436+47443+5669+855928+8113549+424282=82648727
872027+1115415+1+47922+547008+56+5550+71642773+948394=75179146
69724687+8+7+9940+5568+29585+518916=70288711
2415+382376+88324+81544+926+68619475+677+222852=69398589
659157+5144361+7072+16+4799+811+58742059+451875+138174=65148324
6569+59336692+75+11328=59354664
28332+725+2683+45913425+9648987+4911=55599063
66+68232+9406935+6489662=15964895
80+21+4478882+8945092=13424075
5017598+5639262+4+62+9413+4+4991+41568=10712902
8635147+876+5597+1686+54=8643360
8081+8361365+6+477=8369929
35+2983325+0+6400=2989760
2288508+509472+43+83704=2881727
1804642
4977+164+77+1018643+593851+83730=1701442
5+54102+1034451=1088558
555839
37844
33+9872=9905
62+9598=9660
220+6831+26=7077
142
1

我可以用 来做到:sor!n/.*\</。据我了解,\<意思是一个词的开头。然而,为什么不简单地:sor!n/.*=/工作呢?或者,如果=是模式中的特殊字符,那么我希望它:sor!n/.*\=/可以工作,但这会产生NFA regex错误。

答案1

这可能最适合https://vi.stackexchange.com/

但无论如何,:h :sort实际上解释了那里发生的事情:

If a {pattern} is used, any lines which don't have a
match for {pattern} are kept in their current order,
but separate from the lines which do match {pattern}.
If you sorted in reverse, they will be in reverse
order after the sorted lines, otherwise they will be
in their original order, right before the sorted
lines.

简而言之,.*\<匹配输入中的每一行并:sort在所有行上执行。

虽然.*=仅匹配包含 的行=,因此这些行按 排序:sort。所有剩余的行(与模式不匹配的行):

142
1804642
1
37844
555839

未排序,而是直接转储到文件的开头。由于您使用的!所有行的顺序都是相反的,因此它们以相反的顺序在文件末尾结束。

答案2

来补充格罗赫马尔的好答案,你可以使用

:sort!n/.*=\|^/

或者:

:sort!n/[^=]*$/r

以获得想要的结果。

至于为什么\=会给你一个错误,是因为它是vim.与此相同,\?只是它也可以在?命令中使用。与无效的正则表达式.*\=相同。.*\?

相关内容