Vim 同一行只替换匹配字符的偶数项

2021-02-15  本文已影响0人  千羽之城88

比如现在有一个文档:

406,109,420,124,449,103,836,103,862,133,906,99,920,87,910,82,890,73,890,-77,907,-77,927,-45,987,-87,995,-92,994,-95,992,-97,981,-97,386,-97,359,-96,341,-110,307,-77,425,-77,425,72,428,93,406,109,446,396,457,406,494,373,801,373,828,400,870,369,877,363,875,359,874,354,864,352,858,343,858,289,862,182,861,152,820,151,820,175,501,175,506,141,458,128,463,169,465,276,465,365,462,380,446,396,566,843,574,854,650,820,664,815,656,809,640,805,629,791,629,752,803,752,835,784,891,742,899,737,898,734,896,732,885,732,629,732,629,657,882,657,907,686,958,648,962,638,948,639,943,637,937,635,928,642,863,565,850,569,888,637,400,637,400,505,400,217,363,87,326,-40,224,-122,216,-112,315,-5,338,127,362,264,362,487,362,633,360,660,342,680,353,693,384,657,591,657,591,801,593,818,566,843,224,833,234,843,310,800,321,791,311,787,293,786,278,774,241,662,197,573,197,8,199,-87,200,-120,152,-133,157,-94,159,54,159,501,101,401,34,335,22,342,127,498,181,632,236,766,237,791,239,817,224,833,581,618,662,598,667,590,659,587,644,583,634,578,634,535,743,546,763,583,817,548,830,541,827,538,825,535,807,534,634,519,634,476,645,467,656,457,736,457,826,457,832,457,847,459,837,457,850,459,864,461,869,490,872,521,872,542,886,542,888,506,888,463,906,458,891,429,838,427,718,427,632,427,617,438,602,449,596,485,596,516,513,509,479,502,468,487,425,515,596,531,596,587,597,602,577,608,581,618,575,-77,575,87,461,87,461,-77,575,-77,715,-77,715,87,607,87,607,-77,715,-77,747,87,747,-77,852,-77,852,87,747,87,645,279,645,357,501,357,501,279,645,279,679,357,679,279,820,279,820,357,679,357,645,191,645,263,501,263,501,191,645,191,820,191,820,263,679,263,679,191,820,191

返回这样的由一对偶数数据得到的数组:

406,109,
420,124,
...
...
679,191,
820,191

宏的解法

qa
0f,f,a^M
j
q

比如有 2000 行,可以执行:200@a

命令 解释
qa 开始录制宏
0 跳转到行首
f,f, 从行首起找到第二个逗号
a 在逗号后面插入
^M 确定插入
j 跳转下一行
q 关闭录制

vim中输入:Ctrl+V,Enter 得到 ^M

命令行的解法

let a=[',','^M']
:%s/,/\=reverse(a)[0]/g
命令 解释
a=[',',',^M'] 设置一个数组,包含两个元素
=reverse(a) 将数组的元素前后翻转
reverse(a)[0] 取翻转后的数组中的第一个元素
上一篇下一篇

猜你喜欢

热点阅读