Dav*_*ica 21 powershell cat find-and-replace pipe
我想在使用之前测试一个替换,所以我正在尝试编写一个快速的在线命令来查看输出是什么。但是,我不确定语法是什么。我想做的是
cat file | -replace "a", "b"
Run Code Online (Sandbox Code Playgroud)
什么是正确的 powershell 语法?
我知道我也可以做$a = cat file
然后在 上进行替换$a
,但我想将其保留在一行上
shi*_*jai 22
这应该可以解决问题,它将遍历文件中的所有行,并将任何“a”替换为“b”,但之后您需要将其保存回文件中
cat file | % {$_.replace("a","b")} | out-file newfile
Run Code Online (Sandbox Code Playgroud)
小智 8
要使用Powershell -replace 运算符(适用于正则表达式),请执行以下操作:
cat file.txt | % {$_ -replace "\W", ""} # -replace operator uses regex
Run Code Online (Sandbox Code Playgroud)
请注意, -replace 运算符使用正则表达式匹配,而以下示例将使用非正则表达式文本查找和替换,因为它使用.NET Framework的String.Replace 方法
cat file | % {$_.replace("abc","def")} # string.Replace uses text matching
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30289 次 |
最近记录: |