gpt4 book ai didi

Vim如何移动csv列

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

假设一个像这样的经典 csv:

1,2,3,4,5,6,7
1,2,3,4,5,6,7
1,2,3,4,5,6,7
我试图使用简单而优雅的单行将每行的第 6 列移动到行首。
有没有办法使用类似的方法来实现这一点:
g/,/norm 5n<?>d0P
我不知道用什么代替 来选择第 5 个逗号之后的单词

最佳答案

对全局命令的一点修改

:g/./exec 'normal 5f,lvld0P'

:g .............. globally
/./ ............. on each line that has something
exec ............. execute the following normal command
5f, .............. jumpt to the fifht ,
l ................ move to the number
vl ............... select the number and its coma
d ................ cut to the unamed register
0 ................ jump to the beginning of line
P ................ past the content of default register (before)
更新
我们不是简单地选择数字和下一个昏迷,而是选择直到下一次昏迷。 (这是一个更通用的解决方案),避免可能有两位数或更多数字的列出现问题。
g/./exec 'normal 5f,ldf,0P'
使用 gnu awk
awk -i inplace -F, -v OFS="," '{print 6,1,2,3,4,5,7}' target-file

-i inplace ............. no need to use temp file
-F ...................... field separator
-v OFS .................. output field separator
从 vim 调用 awk
:%! awk -F, -v OFS="," '{print 6,1,2,3,4,5,7}'

% ............... current file
! ............... filter trhough external command

关于Vim如何移动csv列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64695959/

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