2016年4月18日月曜日

【PowerShell】Windowsでgrepもどき その3

Windows&PowerShellでgrepもどきをやるさいのメモ その3
※その1はこっち
※その2はこっち

grepしたい文字列を配列で渡して、結果を配列の要素毎にファイルに出力する。

とりあえず例

下記のtextから先頭の文字「a,b,c」毎にgrepし「a.txt,b.txt,c.txt」に保存する。

元となるtext

a:1
b:45
c:7890
a:23
b:6



コマンド

PS D:\@temp> @("a","b","c") | %{ sls $_ .\sample.txt | %{ $_.Line} | Out-File ($_ + ".txt")}


結果はこんな感じ

結果

PS D:\@temp> gc .\a.txt
a:1
a:23
PS D:\@temp> gc .\b.txt
b:45
b:6
PS D:\@temp> gc .\c.txt
c:7890