select-string
PowerShell 에서 Grep 실행
select-string
을 사용합니다.
help
PS F:\TEST\ps> get-help select-string
이름
Select-String
개요
문자열 및 파일에서 텍스트를 찾습니다.
구문
Select-String [-Path] <string[]> [-Pattern] <string[]> [-AllMatches] [-Case Sensitive] [-Context <Int32[]>] [-Encoding <string>] [-Exclude <string[]>] [-Include <string[]>] [-List] [-NotMatch] [-Quiet] [-SimpleMatch] [<CommonP arameters>] Select-String -InputObject <psobject> [-Pattern] <string[]> [-AllMatches] [ -CaseSensitive] [-Context <Int32[]>] [-Encoding <string>] [-Exclude <string []>] [-Include <string[]>] [-List] [-NotMatch] [-Quiet] [-SimpleMatch] [<Co mmonParameters>]
실행
PS F:\TEST\ps> select-string "updateLog" log.log
log.log:2:updateLogStatus Start log.log:4:updateLogStatus END
텍스트 내용에 추가적으로, 파일명과, 줄번호가 함께 출력됩니다.
[파일명]:[줄번호]:[텍스트]
내용만 출력
foreach 구문을 통해서 출력합니다.
PS F:\TEST\ps> select-string "updateLog" log.log | foreach{$_.line} updateLogStatus Start updateLogStatus END
사용가능한 함수는 아래와 같이 확인합니다.
PS F:\TEST\ps> select-string "updateLog" log.log | get-member
TypeName: Microsoft.PowerShell.Commands.MatchInfo
Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() RelativePath Method string RelativePath(string directory) ToString Method string ToString(), string ToString(string directory) Context Property Microsoft.PowerShell.Commands.MatchInfoContext Conte... Filename Property System.String Filename {get;} IgnoreCase Property System.Boolean IgnoreCase {get;set;} Line Property System.String Line {get;set;} LineNumber Property System.Int32 LineNumber {get;set;} Matches Property System.Text.RegularExpressions.Match[] Matches {get;... Path Property System.String Path {get;set;} Pattern Property System.String Pattern {get;set;}