Pascal Programming Example (로그 분석기)
요구사항:
특정 폴더에 ‘zip’ 형식으로 파일의 압축을 해제 하여 라인별로 검색을 진행한다. 사용자 문자열을 포함하는 라인은 출력 파일에 추가하는 형식이다.
Steps
1. 특정 디렉토리의 압축 파일을 해제한다.
2. 텍스트 파일 처리
2.1 파일명이 ‘.log’ 로 끝나는 파일을 라인 단위로 읽는다.
2.2 읽은 라인에서 사용자가 입력한 문자열을 포함하는 라인이 있는지 확인한다.
IF 검색 문자열을 포함하고 있는 경우
THEN 3단계 실행
ELSE 다음 라인 읽기
3. 파일에 검색된 문자열을 쓴다.
2. 포함된 파일에서 ‘.log’로 끝나는 텍스트형태의 파일을 라인 단위로 읽는다.
**\[ChatLogAnalyzer\]**
(* Author: coozplz Date: 2013. 10. 30 Desc: 1. 압축파일 해제 방법 2. txt 파일을 읽어 특정 문자열이 포함된 라인만 추출 3. 추출한 결과를 출력 파일에 입력(append) *) program ChatLogAnalyzer; uses zipper, Classes, sysutils, frmChatLogAnalyzer; { procedure extractZipFiles 경로를 입력받아 해당 파일에 존재하는 모든압축 파일을 해제한다. sourcePath: 압축파일 경로 destPath: 압축 해제 경로 suffix: 압축 파일 형식 } procedure extractZipFiles(sourcePath : String; destPath: String; suffix: String);var unzipper :TUnZipper; fileInfo : TSearchRec;begin unzipper := TUnZipper.Create; SetCurrentDir(sourcePath); try if FindFirst(suffix, faAnyFile, fileInfo) = 0 then begin repeat with fileInfo do begin // 파일 정보 출력 writeln(name:60, size:15); unzipper.FileName := sourcePath+'\'+name; unzipper.OutputPath := sourcePath; unzipper.Examine; unzipper.UnZipAllFiles; end; until FindNext(fileInfo) 0 ; FindClose(fileInfo); end; finally unzipper.Free; end; writeln('Done extract files');end; { method: main } var path : String; fileInfo : TSearchRec; logFile : Text; outputFile : Text; readLine : string; searchText : String;begin path := 'C:\Users\Administrator\Desktop\IBK\20131030'; searchText:= 'ChatFileRelayWorker:269'; extractZipFiles(path, path, '*.zip'); SetCurrentDir(path); if FindFirst('*.0', faAnyFile, fileinfo) = 0 then begin Assign(outputFile, path + '\result.out'); Rewrite(outputFile); repeat writeln(fileInfo.name: 60, fileInfo.size:15); AssignFile(logFile, path + '\' + fileInfo.Name); Reset(logFile); {파일은 처음부터 다시 읽도록 설정} Append(outputFile); {출력 파일은 이전 내용에 붙여넣기를 한다.} // Append(outputFile); while not EOF(logFile) do begin ReadLn(logFile, readLine); //writeln(Pos(searchText, readLine)); if Pos(searchText, readLine) > 0 then begin writeln(outputFile, readLine); end; end; until FindNext(fileInfo) 0; Close(logFile); Close(outputFile); end; end.
**\[frmChatLogAnalyzer\]**
unit frmChatLogAnalyzer; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus, StdCtrls, zipper, TLoggerUnit ; type { TfrmChatLog } TfrmChatLog = class(TForm) btnOpenSelectDir: TButton; btnOpenSaveDialog: TButton; btnExecute: TButton; lb_resultSave: TLabel; SaveDialog1: TSaveDialog; SelectDirectoryDialog1: TSelectDirectoryDialog; txtSearchValue: TEdit; lb_searchText: TLabel; txtSourceDir: TEdit; Label1: TLabel; txtResultFilePath: TEdit; procedure btnExecuteClick(Sender: TObject); procedure btnOpenSaveDialogClick(Sender: TObject); procedure btnOpenSelectDirClick(Sender: TObject); private { private declarations } public { public declarations } end; var frmChatLog: TfrmChatLog; sourcePath : String; {압축 파일 저장경로} destPath: String; {결과 파일 저장 경로} fileInfo : TSearchRec; {압축 파일 정보} logFile : Text; {압축 해제 파일} resultFile : Text; {파일 쓰기 정보} readLine : string; {텍스트 1줄} searchText : String; {검색어} implementation {$R *.lfm} { TfrmChatLog } { procedure extractZipFiles 경로를 입력받아 해당 파일에 존재하는 모든압축 파일을 해제한다. sourcePath: 압축파일 경로 destPath: 압축 해제 경로 suffix: 압축 파일 형식 } procedure extractZipFiles(sourcePath : String; suffix: String);var unzipper :TUnZipper; fileInfo : TSearchRec;begin unzipper := TUnZipper.Create; SetCurrentDir(sourcePath); try if FindFirst(suffix, faAnyFile, fileInfo) = 0 then begin repeat with fileInfo do begin // 파일 정보 출력 writeln(name:60, size:15); unzipper.FileName := sourcePath+'\'+name; unzipper.OutputPath := sourcePath; unzipper.Examine; unzipper.UnZipAllFiles; end; until FindNext(fileInfo) 0 ; FindClose(fileInfo); end; finally unzipper.Free; end; writeln('Done extract files');end; procedure TfrmChatLog.btnOpenSelectDirClick(Sender: TObject);begin if SelectDirectoryDialog1.Execute then begin writeln('sourcePath: '+SelectDirectoryDialog1.FileName); sourcePath:= SelectDirectoryDialog1.FileName; txtSourceDir.Text:=sourcePath; end;end; procedure TfrmChatLog.btnOpenSaveDialogClick(Sender: TObject);begin if SaveDialog1.Execute then begin writeln('savePath: '+SaveDialog1.FileName); destPath:= SaveDialog1.FileName; txtResultFilePath.Text:=destPath; end; end; procedure TfrmChatLog.btnExecuteClick(Sender: TObject);begin if txtSearchValue.Text = '' then begin ShowMessage('검색어는 필수 항목입니다.'); end; searchText:= txtSearchValue.Text; writeln('SearchText: '+ searchText); extractZipFiles(sourcePath, '*.zip'); SetCurrentDir(sourcePath); if FindFirst('*.0', faAnyFile, fileinfo) = 0 then begin AssignFile(resultFile, destPath); Rewrite(resultFile); repeat writeln(fileInfo.name: 60, fileInfo.size:15); AssignFile(logFile, sourcePath + '\' + fileInfo.Name); Reset(logFile); {파일은 처음부터 다시 읽도록 설정} Append(resultFile); {출력 파일은 이전 내용에 붙여넣기를 한다.} // Append(outputFile); while not EOF(logFile) do begin ReadLn(logFile, readLine); //writeln(Pos(searchText, readLine)); if Pos(searchText, readLine) > 0 then begin writeln(resultFile, readLine); end; end; until FindNext(fileInfo) 0; CloseFile(logFile); CloseFile(resultFile); end; ShowMessage('Finish Analyze'); Close;end;end.