2017-01-06 14 views
2

検索する列名を指定せずに行単位で.CSVファイルを検索する方法はありますか?複数の.CSVファイルでスクリプトを作成するので、列名を指定することは私にとってはオプションではありません。Powershellで列名を指定しないで特定の値を見つけるためにCSVファイルを検索

サンプルPowerShellのコード:

foreach ($row in $csvFile){ 
    if ($row -eq/-contains $StringIWantToFind) { 
     #do something with the string here 
    } 
} 

答えて

4

あなたは(サブ)文字列はワイルドカード一致使用することができますにあるcolumを気にしない場合:

$row -like "*$StringIWantToFind*" 

または正規表現マッチを:

$row -match $StringIWantToFind 

この値は、自動変数$matches経由の事、それはあなたが一致しています(とサブマッチ)ので:

$StringIWantToFind = 'something (captured group) or other' 

foreach ($row in $csvFile) { 
    if ($row -match $StringIWantToFind) { 
     # do something with $matches[0] (full match) or $matches[1] (captured 
     # group) here 
    } 
} 
1

これを実行する最も簡単な方法は、Select-Stringを使用することです、このような何か:

Select-String your_file.txt -Pattern 'string to find' -SimpleMatch 

あなたの場合結果を処理したい場合は、一致した行を抽出することができます。

Select-String your_file.txt -Pattern 'xx' -SimpleMatch | Select -ExpandProperty line | % { 
    # your processing here using $_ 
} 
関連する問題