gpt4 book ai didi

vim - 如何在 Vim 中删除指定符号后直到行尾的文本?

转载 作者:行者123 更新时间:2023-12-04 01:27:01 24 4
gpt4 key购买 nike

我的数据格式如下:

id|name|things that I don't care
因此,对于每一行,我想在第二个 | 之后删除文本符号。
如何使用一个命令在 Vim 中执行此操作?
注:在第二个 | 之后实际上还有额外的管道, 自 |字符用作列的分隔符。

最佳答案

一种解决方案:

:%s!^\([^|]*|\)\{2\}\zs.*!!

说明:
  • % : 每一行
  • s : 字幕
  • ! : 模式开始
  • ^ : 行首
  • \( : 组开始
  • [^|]* : 任意数量的非管道字符
  • | : 后跟一个管道
  • \) : 群结束
  • \{2\} : 匹配该组的两个计数
  • \zs : 从这里开始模式匹配
  • .* : 任何字符
  • ! : 模式结束和替换开始
  • ! : 更换结束

  • 这将使少于两个管道的行保持不变,并且还将处理多于两个管道的行......

    之前
    id name things that I don't care no pipes
    id|name things that I don't care one pipe
    id|name|things that I don't care two pipes
    id|name|things that I don't care extra pipe at line end|
    id|name|things that I don't care | extra pipe mid-line
    id|name|things that I don't| care| two extra pipes
    name|things that I don't care missing first column and pipe
    |name|things that I don't care missing first column

    :
    id name things that I don't care no pipes
    id|name things that I don't care one pipe
    id|name|
    id|name|
    id|name|
    id|name|
    name|things that I don't care missing first column and pipe
    |name|

    关于vim - 如何在 Vim 中删除指定符号后直到行尾的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7459677/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com