如何在 Vim 中删除指定符号后直到行尾的文本? [英] How to delete text after a specified symbol until the end of the line in Vim?

查看:55
本文介绍了如何在 Vim 中删除指定符号后直到行尾的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据格式为:

id|name|things that I don't care

所以对于每一行,我想删除第二个 | 符号之后的文本.

So for each line I want to delete text after the second | symbol.

如何在 Vim 中使用一个命令执行此操作?

How do I do this in Vim with one command?

注意:在第二个 | 之后实际上有 个额外的管道,因为 | 字符被用作列的分隔符.

Note: There are actually extra pipes after the second |, since the | character is used as the delimiter for columns.

推荐答案

一个解决方案:

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

说明:

  • %:每一行
  • s:字幕
  • !:模式开始
  • ^:行首
  • \(:组开始
  • [^|]*:任意数量的非管道字符
  • |:后跟一个管道
  • \):组结束
  • \{2\}:匹配该组的两个计数
  • \zs:启动模式匹配在这里
  • .*:任意字符
  • !:模式结束和替换开始
  • !:替换结束
  • %: on every line
  • s: subtitute
  • !: start of pattern
  • ^: start of line
  • \(: start of group
  • [^|]*: any number of non-pipe characters
  • |: followed by a pipe
  • \): end of group
  • \{2\}: match two counts of that group
  • \zs: start the pattern matching here
  • .*: any characters
  • !: end of pattern and start of replacement
  • !: end of replacement

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

This will leave lines with fewer than two pipes untouched and will also deal with lines that have more than the two pipes...

之前

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 中删除指定符号后直到行尾的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆