[ファイルから同一データを取り除く]
テキストファイルの中に、同一データが複数含まれている際に、重複データを取り除きます。
プログラム実行前にSORT処理を行い、前後のデータが同一か判断しながら結果を出力します。
【ソースコード】 [tips0069.pas]
program tips0069;
{$APPTYPE CONSOLE}
uses SysUtils;
var
f:TextFile;
strA:string;
strB:String;
cnt1,cnt2:Integer;
begin
if ParamCount<>1 then begin
WriteLn('入力パラメータに誤りがあります。');
Exit;
end;
AssignFile(f,ParamStr(1));
Reset(f);
cnt1:=0; cnt2:=0;
strA:='';
ReadLn(f,strB); cnt1:=cnt1+1;
While not eof(f) do begin
if strA = strB then
begin
ReadLn(f,strB); cnt1:=cnt1+1;
end
else
begin
WriteLn(strB) ; cnt2:=cnt2+1;
strA:=strB;
ReadLn(f,strB); cnt1:=cnt1+1;
end;
end;
if strA <> strB then
begin
WriteLn(strB) ; cnt2:=cnt2+1;
end;
WriteLn('read count:', cnt1);
WriteLn('write count:', cnt2);
end.
【コンパイル&実行】 [tips0069.bat]
dcc32 tips0069.pas
sort tips0069_input.txt /o tips0069_sortout.txt
tips0069.exe tips0069_sortout.txt > tips0069.txt
pause
【入力データ】 [tips0069_input.txt]
aaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbb
aaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbb
aaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbb
cccccccccccccccc
【実行結果】 [tips0069.txt]
aaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbb
cccccccccccccccc
read count:10
write count:3
Copyright c 2009, ohishi,All rights reserved.