加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在powerhell中用正则表达式替换文本文件的内容

发布时间:2020-12-14 06:33:24 所属栏目:百科 来源:网络整理
导读:我有一个简单的文本文件,我需要一个powershell脚本来替换文件内容的一些部分。 我目前的脚本如下: $content = Get-Content -path "Input.json"$content -Replace '"(d+),(d{1,})"','$1.$2' | Out-File "output.json" 是否可以在一行中编写没有内容变量的
我有一个简单的文本文件,我需要一个powershell脚本来替换文件内容的一些部分。

我目前的脚本如下:

$content = Get-Content -path "Input.json"

$content -Replace '"(d+),(d{1,})"','$1.$2' |  Out-File "output.json"

是否可以在一行中编写没有内容变量的内容?

Get-Content -path "Input.json" | ??? -Replace '"(d+),'$1.$2' |  Out-File "output.json"

我不知道如何在没有$ content变量的第二个命令中使用第一个get-content命令行的输出?有一个自动的powerhell变量

有可能做更多的替代,而不是一个管道。

Get-Content -path "Input.json" | ??? -Replace '"(d+),'$1.$2' | ??? -Replace 'second regex','second replacement' |  Out-File "output.json"
是的,你可以在一行中做到这一点,甚至不需要一个管道,因为-replace在数组上工作,就像你期望的那样(你可以链接操作符):
(Get-Content Input.json) `
    -replace '"(d+),'$1.$2' `
    -replace 'second regex','second replacement' |
  Out-File output.json

(换行换行可读性)

Get-Content调用周围的括号需要防止-replace操作符被解释为Get-Content的参数。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读