Vi:仅删除非第一次(第二次、第三次)出现的多行

发布于 2025-01-14 15:09:56 字数 333 浏览 5 评论 0原文

如何删除文本文件中除第一行之外的所有多行? (此问题可能相关。) 我需要保留顺序,否则我已经使用了 :sort u

示例:

hsdf
asdf
csdf
csdf
hsdf
dsdf
jsdf
asdf

结果

hsdf
asdf
csdf
dsdf
jsdf

代替

asdf
csdf
dsdf
hsdf
jsdf

How to delete every occurence of multiple lines in a text file except the first one? (This question might be related.)
I need to keep the order, otherwise I had used :sort u.

Example:

hsdf
asdf
csdf
csdf
hsdf
dsdf
jsdf
asdf

results in

hsdf
asdf
csdf
dsdf
jsdf

instead of

asdf
csdf
dsdf
hsdf
jsdf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脸赞 2025-01-21 15:09:56

有时候你不需要想太多。只要拿起那把大锤子,敲一下就可以了。

那么计划是什么?循环所有行;将它们存储到关联数组中;如果它已经存在,则将其从缓冲区中删除。看起来很愚蠢,第一次尝试就能让事情发挥作用:

:let foo = {}
:g/./if foo->has_key(getline(".")) | delete | else | let foo[getline(".")] = 1 | endif
:unlet foo

Sometimes you don't need to think much. Just take that big hammer and make a bang.

So what's the plan? Loop over all lines; store them into associative array; if it's already there then delete it from buffer. Looks dumb enough to get things working by the first attempt:

:let foo = {}
:g/./if foo->has_key(getline(".")) | delete | else | let foo[getline(".")] = 1 | endif
:unlet foo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文