0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PowerShell】変な位置でカンマ区切りされたtxtに対して(区切られる前にあるべき)特定文字列の有無を確認する

Posted at

環境

  • PowerShell5.1

状況

  • 意図しない位置でカンマ区切りされたtxtファイル達の中から、特定の文字列が含まれるファイルを列挙する
  • 「意図しない位置でカンマ区切りされた」とは例えば以下のような場合を指す "20","18/1","0/01:晴れ"
  • 例として、"2018/10/01"または"good"の文字列を持つファイルを抽出する。
# テストファイルを作成する
New-Item truecase1.txt -Value """20"",""18/1"",""0/01:晴れ"
New-Item truecase2.txt -Value """20"",""18/1"",""0/02:good"
New-Item falsecase1.txt -Value """20"",""18/1"",""0/02:bad"

# メインのロジック。
# カンマ区切りされた後の文字列の正規表現を作成し、ファイル名を出力
$td = "2018/10/01|good"
$ans = ""
$sep = "("","")*"
foreach ($c in $td.ToCharArray()) {$ans+=$c + $sep}
Select-String -Path "*.txt" -Pattern $ans | Select-Object filename | Get-Unique -AsString

# テストファイルを消去する
Remove-Item .\truecase1.txt
Remove-Item .\truecase2.txt
Remove-Item .\falsecase1.txt
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?