3
5

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 でファイル内の文字列をワンライナーで置換する

Posted at
コード例
(Get-Content /path/to/file) -replace "置換したい文字列","置換する文字列"

複数の文字列を置換したい場合

そのまま後ろに -replace を追加するだけ

コード例
(Get-Content /path/to/file)
  -replace "置換したい文字列","置換する文字列"
  -replace "置換したい文字列2","置換する文字列2"

正規表現を使う場合

-replace 内でそのまま正規表現が使える

コード例
(Get-Content /path/to/file) -replace "<body>.+?</body>",""

変換後の内容をファイルに保存

そのままリダイレクトすればいい

コード例
(Get-Content /path/to/file) -replace "置換したい文字列","置換する文字列" > /path/to/file/replaced
3
5
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
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?