facebook twitter hatena line email

Windows/powershell

提供: 初心者エンジニアの簡易メモ
2021年8月12日 (木) 14:07時点におけるAdmin (トーク | 投稿記録)による版 (エスケープ文字対応)

移動: 案内検索

powershellインストール

https://docs.microsoft.com/ja-jp/powershell/scripting/install/installing-powershell-core-on-macos?view=powershell-7.1

macであれば、powershell-x.x.x-osx-x64.pkg をDL

macでpowershell実行

$ /usr/local/bin/pwsh /tmp/hoge.ps1

/tmp/hoge.ps1

echo 'hoge'

変数化

$path = "hoge.txt";
$data = Get-Content $path

テキスト文置換

aaaをcccへ置換

$data = Get-Content hoge.txt | % { $_ -replace "aaa", "ccc" }
$data | Out-File hoge.txt

参考:https://teratail.com/questions/290149

エスケープ文字対応

[]などは、\でエスケープする

$data = Get-Content hoge.txt | % { $_ -replace "aaa\[hoge\]", "ccc[piyo]" }