簡易Grep
はじめて扱うコマンドレットとかあると、作るのに時間が結構掛った orz
練習しないとぱっと作れないなーと実感。
$exp = '[正規表現 検索キーワード]' $extension = '[ファイル名 正規表現フィルター]' $target = "[検索対象フォルダ]" cls #dir -Path $target | dir -Path $target -Recurse | ? { $pos = 0; $file=$_.FullName; $_.Name -match $extension } | % { try { cat $_.FullName } catch [Exception] { <# 何もしない #> } } | ? { ++$pos; $_ -match $exp } | % { $pos; $file; $_ }
■F#版
思ったように作れなかった。。
PowerShellより面倒くさいと感じます。
let exp = @"[検索キーワード]" let expression = @"[ファイル名 フィルター]" let target = @"検索対象フォルダ" open System.IO open System.Text.RegularExpressions let m p i = Regex.IsMatch(i,p) let rec dir path = //Directory.GetDirectories(path, "*", SearchOption.AllDirectories) seq { yield! Directory.EnumerateFiles path for s in Directory.EnumerateDirectories path do yield! dir s } let readFile (path:string) = try seq { use sr = new StreamReader(path) while not sr.EndOfStream do yield sr.ReadLine() } |> Seq.mapi(fun i t -> (i+1, t, path)) |> Seq.filter(fun (_,t,_) -> m exp t) |> Seq.iter(printfn "%A") with | _ -> () // Main dir target |> Seq.filter(m expression) |> Seq.iter(readFile) stdin.Read() |> ignore
■参考サイト
PowerShell_ファイル入出力
プログラミング言語の比較 > ファイルとディレクトリ、通信
-
- -