如何在Vim中用编号标签替换CSV列分隔符? [英] How to replace CSV column separators with numbered labels in Vim?

查看:156
本文介绍了如何在Vim中用编号标签替换CSV列分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用不同的值替换一系列管道字符。

I want to replace a series of pipeline characters with different values. How would I do this with regular expressions?

示例:

This | is | a | sentence
And | this | is | the | second | one

最终结果:

This new is new2 a new3 sentence
And new this new2 is new3 the new4 second new5 one


推荐答案

如果替换值仅在结尾处不同,请使用命令

If substitution values differ only in the numbers at the ends, use the command

:let n=[0] | %s/|/\='new'.map(n,'v:val+1')[0]/g

(请参阅我的回答
的问题 gVim查找/替换计数器

如果替换值本质上彼此不同,请更改
命令以替换事件的序列号而不是项目

In case of substitution values that differ essentially from each other, change the command to substitute not a serial number of an occurrence, but an item of a replacement list with that number as an index.

:let n=[-1] | %s/|/\=['one','two','three'][map(n,'v:val+1')[0]]/g

要在每一行上彼此独立执行替换,请使用命令通过缓冲区的
行迭代上述命令之一。

To perform the substitutions on every line independently of each other, use the :global command to iterate one of the above commands through the lines of a buffer.

:g/^/let n=[0] | s/|/\='new'.map(n,'v:val+1')[0]/g

类似地,

:g/^/let n=[-1] | s/|/\=['one','two','three'][map(n,'v:val+1')[0]]/g

这篇关于如何在Vim中用编号标签替换CSV列分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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