「Windows/powershell/文字列」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==文字列検索== <pre> $str = 'hogepiyo' if ($str.IndexOf('hoge') > -1) { Write-Host "hit " + $str.IndexOf('hoge') # 0 } else { Write-Host "no hit" } </pre>」) |
|||
| 行1: | 行1: | ||
| + | ==文字列内に変数== | ||
| + | echo "hoge=$hoge" | ||
| + | echo "hoge=${hoge}" | ||
| + | echo ('hoge=' + $hoge) | ||
| + | 公式参考:https://docs.microsoft.com/ja-jp/powershell/scripting/learn/deep-dives/everything-about-string-substitutions?view=powershell-7.1 | ||
| + | |||
| + | ==テキスト置換== | ||
| + | $name = $name.Replace('taro', 'jiro') | ||
| + | |||
| + | ==ファイル内のテキスト文置換== | ||
| + | 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]" } | ||
| + | |||
==文字列検索== | ==文字列検索== | ||
<pre> | <pre> | ||
2021年8月30日 (月) 20:48時点における最新版
文字列内に変数
echo "hoge=$hoge"
echo "hoge=${hoge}"
echo ('hoge=' + $hoge)
テキスト置換
$name = $name.Replace('taro', 'jiro')
ファイル内のテキスト文置換
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]" }
文字列検索
$str = 'hogepiyo'
if ($str.IndexOf('hoge') > -1) {
Write-Host "hit " + $str.IndexOf('hoge') # 0
} else {
Write-Host "no hit"
}
