vim如何组合包含相同id的行
我有一个csv文件,我想合并具有相同id的行。示例:vim如何组合包含相同id的行,vim,neovim,Vim,Neovim,我有一个csv文件,我想合并具有相同id的行。示例: ID,Name 1113,Firefox 1114,Chrome 1113,InternetExplorer 它应该是这样的: ID,Name 1113,"Firefox,InternetExplorer" 1114,Chrome 谢谢Awk可能是您的解决方案,像这样的东西可能就足够了: :2,$!awk -F, '{ a[$1] = (a[$1] ? a[$1] "," : ""
ID,Name
1113,Firefox
1114,Chrome
1113,InternetExplorer
它应该是这样的:
ID,Name
1113,"Firefox,InternetExplorer"
1114,Chrome
谢谢Awk可能是您的解决方案,像这样的东西可能就足够了:
:2,$!awk -F, '{ a[$1] = (a[$1] ? a[$1] "," : "") $2 } END { for (p in a) print p "," a[p] }'
它将连接第一列上的行,并用逗号连接所有第二列:
ID,Name
1113,Firefox,InternetExplorer
1114,Chrome
第二列在输出时不被引用,也不保证排序。对于VIM方法
约束:
当第2列包含
{column 1}的值时,将不起作用,
这是我搜索的内容,谢谢!
:2,$sort
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
1000@a ---> execute 1000 times