我们说我有一个格式如下的CSV文件:
function cell2csv(filename, cellArray, delimiter)
% Writes cell array content into a *.csv file.
%
% CELL2CSV(filename,cellArray,delimiter)
%
% filename = Name of the file to save. [ i.e. 'text.csv' ]
% cellarray = Name of the Cell Array where the data is in
% delimiter = seperating sign, normally:',' (default)
%
% by Sylvain Fiedler, KA, 2004
% modified by Rob Kohr, Rutgers, 2005 - changed to english and fixed delimiter
if nargin<3
delimiter = ',';
end
datei = fopen(filename,'w');
for z=1:size(cellArray,1)
for g=1:length(cellArray{z})
var = eval(['cellArray{z}{g}']);
if size(var,1) == 0
var = '';
end
if isnumeric(var) == 1
var = num2str(var);
end
fprintf(datei,var);
end
fprintf(datei,'\n');
end
fclose(datei);
我需要这样做:
1,Mark
1,Smith
2,Janet
2,Smith
我需要在第一行之后替换任何给定行开头的每一个出现,然后将没有开启者的任何行连接到最后一行。我怎么能这样做?
答案 0 :(得分:1)
将下一行附加到图案空间并删除换行符后跟一个数字:
Dispatcher
如果只出现一次或两次以上:
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public ReadOnly Property Dispatcher As Dispatcher
答案 1 :(得分:0)
使用awk更容易,无论您有多少次出现或无论数据是否已排序,都可以正常工作:
awk 'BEGIN{FS=OFS=","}{!a[$1]?a[$1]=$0:a[$1]=a[$1] " " $2}END{for (i in a) print a[i]}' file1