vim 如何合并包含相同 ID 的行

vim how to combine lines containing same id

我有一个 csv 文件,我想合并具有相同 ID 的行。示例:

ID,Name
1113,Firefox
1114,Chrome
1113,InternetExplorer

它应该看起来像:

ID,Name
1113,"Firefox,InternetExplorer"
1114,Chrome

谢谢

Awk 可能是适合您的解决方案,像这样可能就足够了:

:2,$!awk -F, '{ a[] = (a[] ? a[] "," : "")  } END { for (p in a) print p "," a[p] }'

它会在第一列连接行,并用逗号连接所有第二列:

ID,Name
1113,Firefox,InternetExplorer
1114,Chrome

第二列输出时不加引号,也不保证排序。

对于VIM方法,

  1. 从第二行到文件末尾排序。
:2,$sort
  1. 创建 Marco 'a' 从第二行开始
qa              ---> Record Macro 'a' 
vw"1y           ---> Copy column 1 value including "," to register '1'
V               ---> Select whole line
G?<CR>1<Enter>  ---> Go to end of file then search backward for last occurrence of 
                     register '1' to select all lines with same column 1 value
:'<,'>join      ---> Join selected lines 
:.s/ <CR>1/,/ge ---> Replace register '1' with a space in front to "," 
0wa             ---> Go to begin of line and move to the "," then append
"               ---> Append "
<ESC>$a         ---> Back to command mode and go to end of line then append 
"               ---> Append "
<ESC>j0         ---> Back to command mode and move 1 line down and go to start of line
1               ---> Complete Marco

然后播放宏 'a' 任意次数

1000@a          ---> execute 1000 times

限制条件:
当第 2 列包含值 {column 1},

时将不起作用