傳回MatchInfo物件 -NotMatch 參數 -List 參數 -Quiet 參數 -Context 參數 -AllMatches 參數
取得字串陣列 PS R:\> $arrFullName = (dir *.jpg).FullName PS R:\> $arrFullName R:\1.jpg R:\2.jpg R:\3.jpg R:\4.jpg R:\5.jpg PS R:\>
傳回MatchInfo物件
有幾個(or 幾行)符合,就傳回幾個MatchInfo物件 PS R:\> $arrFullName | Select-String -Pattern '2\.jpg','3\.jpg' R:\2.jpg R:\3.jpg PS R:\> $r = $arrFullName | Select-String -Pattern '2\.jpg','3\.jpg' PS R:\> $r.Count 2 PS R:\>
傳回一個物件 PS R:\> $r = $arrFullName | Select-String -Pattern '2\.jpg' PS R:\> $r.gettype().name MatchInfo PS R:\> $r.Line R:\2.jpg PS R:\> $r[0].Line R:\2.jpg PS R:\>
傳回二個物件 PS R:\> $r = $arrFullName | Select-String -Pattern '2\.jpg','3\.jpg' PS R:\> $r.Count 2 PS R:\> $r.gettype().name Object[] PS R:\> $r[0].gettype().name MatchInfo PS R:\> $r.Line R:\2.jpg R:\3.jpg PS R:\> $r[0].Line R:\2.jpg PS R:\> $r[1].Line R:\3.jpg PS R:\>
-NotMatch 參數
反向選擇 PS R:\> $arrFullName | Select-String -Pattern '2\.jpg','3\.jpg' -NotMatch R:\1.jpg R:\4.jpg R:\5.jpg PS R:\> $r = $arrFullName | Select-String -Pattern '2\.jpg','3\.jpg' -NotMatch PS R:\> $r.Count 3 PS R:\>
-List 參數
預設:列出每一個符合的行 PS R:\> Select-String -Pattern 'b','c' -Path *.txt 1.txt:2:bbb 1.txt:3:ccc 2.txt:2:bbb PS R:\>
-List:每個檔案只列出一次(第一個符合的行) PS R:\> Select-String -Pattern 'b','c' -Path *.txt -List 1.txt:2:bbb 2.txt:2:bbb PS R:\>
-List適用於:資料來源是檔案 資料來源不是檔案,不受影響 PS R:\> $arrFullName | Select-String -Pattern '2\.jpg','3\.jpg' R:\2.jpg R:\3.jpg PS R:\> $arrFullName | Select-String -Pattern '2\.jpg','3\.jpg' -List R:\2.jpg R:\3.jpg PS R:\>
-Quiet 參數
傳回 $true 或 $false PS R:\> $arrFullName | Select-String -Pattern '2\.jpg','3\.jpg' -Quiet True PS R:\>
-Quiet -List (最多只傳回一個MatchInfo) PS R:\> $arrFullName | Select-String -Pattern '2\.jpg','3\.jpg' -Quiet -List R:\2.jpg PS R:\> Select-String -Pattern 'b','c' -Path *.txt -Quiet -List 1.txt:2:bbb PS R:\>
-Context 參數
保留上下2行 PS R:\> $arrFullName | Select-String -Pattern '3\.jpg' -Context 2 R:\1.jpg R:\2.jpg > R:\3.jpg R:\4.jpg R:\5.jpg PS R:\>
保留上1行、下2行 PS R:\> $arrFullName | Select-String -Pattern '3\.jpg' -Context 1,2 R:\2.jpg > R:\3.jpg R:\4.jpg R:\5.jpg PS R:\>
不會因為保留上下行,而多傳回物件 PS R:\> $r = $arrFullName | Select-String -Pattern '3\.jpg' -Context 1,2 PS R:\> $r.count 1 PS R:\> $r.gettype().name MatchInfo
保留上下行的資料,儲存在MatchInfo物件的Context屬性 PS R:\> $r.Context.PreContext R:\2.jpg PS R:\> $r.Context.PostContext R:\4.jpg R:\5.jpg PS R:\>
MatchInfo → 一個字串 PS R:\> "$r" R:\2.jpg > R:\3.jpg R:\4.jpg R:\5.jpg PS R:\> $r | Out-String R:\2.jpg > R:\3.jpg R:\4.jpg R:\5.jpg PS R:\>
MatchInfo → 檔案 PS R:\> $r > a.txt PS R:\> gc a.txt R:\2.jpg > R:\3.jpg R:\4.jpg R:\5.jpg PS R:\> (gc a.txt).Count 5 PS R:\>
MatchInfo | %{$_} PS R:\> $r | %{$_; '=========='} R:\2.jpg > R:\3.jpg R:\4.jpg R:\5.jpg ========== $r是一個物件,只傳進管線一次 PS R:\>
MatchInfo | Select-String PS R:\> $r | Select-String -Pattern '.*' > R:\3.jpg 不會讀取「保留的上下行」 PS R:\>
使用Line屬性,取得那一行內容 (最前面沒有 >) PS R:\> $r | Select-String -Pattern '.*' -InputObject {$_.Line} R:\3.jpg PS R:\>
-AllMatches 參數
預設:每個符合行之內,只找第一個符合處 PS R:\> $s = 'a12345------a789' PS R:\> $s | Select-String -Pattern 'a\d+' a12345------a789 PS R:\> $r = $s | Select-String -Pattern 'a\d+' PS R:\> $r.Count 1 PS R:\> $r.Matches Groups : {0} Success : True Name : 0 Captures : {0} Index : 0 Length : 6 Value : a12345 PS R:\>
每個符合行之內,找到每個符合處 仍然:「一個符合行,只傳回一個MatchInfo」 PS R:\> $r = $s | Select-String -Pattern 'a\d+' -AllMatches PS R:\> $r.Count 1 PS R:\> $r.Matches Groups : {0} Success : True Name : 0 Captures : {0} Index : 0 Length : 6 Value : a12345 Groups : {0} Success : True Name : 0 Captures : {0} Index : 12 Length : 4 Value : a789 PS R:\>
 
沒有留言:
張貼留言