PowerShell替换文件中的值

时间:2019-02-15 09:54:31

标签: powershell

我真的是Powershell的新手。我需要替换文件中的值(始终在变化)。 该值如下所示:

"version": "2.4.5",

我希望替换这个数字。 我有这段代码:

(Get-Content .\bower.json).replace('2.4.5', $version) | Set-Content .\bower.json

但这要求版本号始终为2.4.5,然后再将其设置为新版本。如您所知,下次该数字将不会相同。

有人知道我可以替换版本号而不管它是什么吗?也许使用正则表达式?


玩了一段时间之后,我尝试了这个:

$pattern = '\"version\": \"(.*)\"'
$replace = """version"": ""$version""";
(Get-Content .\bower.json).replace($pattern, $replace) | Set-Content .\bower.json

但是它不能代替任何东西:( 如果我运行:

$replace -match $pattern

它返回“ True”,所以我希望它可以正确替换吗? 这是我的 bower.json 文件:

{
  "name": "sapphire",
  "version": "2.4.5",
  "dependencies": {
    "angular": "~1.7.4",
    "angular-animate": "~1.7.4",
    "angular-barcode": "0.0.4",
    "angular-bootstrap": "2.5.0",
    "angular-cookies": "~1.7.4",
    "angular-google-maps": "2.4.1",
    "angular-loading-bar": "0.9.0",
    "angular-mocks": "~1.7.4",
    "angular-resource": "~1.7.4",
    "angular-sanitize": "~1.7.4",
    "angular-simple-cache": "1.0.6",
    "angular-touch": "~1.7.4",
    "angular-ui-mask": "1.8.7",
    "angular-ui-select": "0.19.8",
    "bootstrap-sass-official": "3.3.7",
    "ng-idle": "1.3.2",
    "ng-notify": "0.8.0",
    "vkbeautify": "*",
    "ngclipboard": "~2.0.0",
    "angular-confirm": "angular1-confirm#1.1.0",
    "angular-ui-router": "~1.0.20",
    "font-awesome": "^5.6.1",
    "ng-confirm": "angular-confirm#^1.1.0"
  },
  "devDependencies": {},
  "appPath": "src",
  "moduleName": "sapphire",
  "overrides": {
    "bootstrap": {
      "main": [
        "less/bootstrap.less",
        "dist/css/bootstrap.css",
        "dist/js/bootstrap.js"
      ]
    }
  },
  "resolutions": {
    "angular": "~1.7.4",
    "angular-animate": "~1.7.4",
    "angular-cookies": "~1.7.4",
    "angular-mocks": "~1.7.4",
    "angular-resource": "~1.7.4",
    "angular-sanitize": "~1.7.4",
    "angular-touch": "~1.7.4",
    "components-font-awesome": "~5.0.6",
    "ngclipboard": "~2.0.0",
    "clipboard": "^2.0.0",
    "angular-ui-router": "~1.0.20"
  }
}

2 个答案:

答案 0 :(得分:2)

由于您具有有效的JSON文件,因此更好的方法似乎是使用它。 [咧嘴]此代码执行以下操作...

  • 无法读取JSON文件
    我不想为此创建一个测试文件。
  • 使用ConvertFrom-JSON将其转换为PSCustomObject
  • 显示导入的.Version属性值
  • 更改该值
  • 显示.Version道具的当前值
  • 将对象转换回JSON并将其写入文件

这是代码...

# fake reading in a text file
#    in real life, use Get-Content
$InStuff = @'
{
  "name": "sapphire",
  "version": "2.4.5",
  "dependencies": {
    "angular": "~1.7.4",
    "angular-animate": "~1.7.4",
    "angular-barcode": "0.0.4",
    "angular-bootstrap": "2.5.0",
    "angular-cookies": "~1.7.4",
    "angular-google-maps": "2.4.1",
    "angular-loading-bar": "0.9.0",
    "angular-mocks": "~1.7.4",
    "angular-resource": "~1.7.4",
    "angular-sanitize": "~1.7.4",
    "angular-simple-cache": "1.0.6",
    "angular-touch": "~1.7.4",
    "angular-ui-mask": "1.8.7",
    "angular-ui-select": "0.19.8",
    "bootstrap-sass-official": "3.3.7",
    "ng-idle": "1.3.2",
    "ng-notify": "0.8.0",
    "vkbeautify": "*",
    "ngclipboard": "~2.0.0",
    "angular-confirm": "angular1-confirm#1.1.0",
    "angular-ui-router": "~1.0.20",
    "font-awesome": "^5.6.1",
    "ng-confirm": "angular-confirm#^1.1.0"
  },
  "devDependencies": {},
  "appPath": "src",
  "moduleName": "sapphire",
  "overrides": {
    "bootstrap": {
      "main": [
        "less/bootstrap.less",
        "dist/css/bootstrap.css",
        "dist/js/bootstrap.js"
      ]
    }
  },
  "resolutions": {
    "angular": "~1.7.4",
    "angular-animate": "~1.7.4",
    "angular-cookies": "~1.7.4",
    "angular-mocks": "~1.7.4",
    "angular-resource": "~1.7.4",
    "angular-sanitize": "~1.7.4",
    "angular-touch": "~1.7.4",
    "components-font-awesome": "~5.0.6",
    "ngclipboard": "~2.0.0",
    "clipboard": "^2.0.0",
    "angular-ui-router": "~1.0.20"
  }
}
'@

$NewVersion = '6.6.6'

$FromJSON = $InStuff |
    ConvertFrom-Json

# show the current imported value
$FromJSON.Version

$FromJSON.Version = $NewVersion

# show the new value
$FromJSON.Version

# send it to a file    
$FromJSON |
    ConvertTo-Json |
    Set-Content -LiteralPath "$env:TEMP\r3plica_-_NewVersion.json"

屏幕输出...

2.4.5
6.6.6

新文件的内容...

{
    "name":  "sapphire",
    "version":  "6.6.6",
    "dependencies":  {
                         "angular":  "~1.7.4",
                         "angular-animate":  "~1.7.4",
                         "angular-barcode":  "0.0.4",
                         "angular-bootstrap":  "2.5.0",
                         "angular-cookies":  "~1.7.4",
                         "angular-google-maps":  "2.4.1",
                         "angular-loading-bar":  "0.9.0",
                         "angular-mocks":  "~1.7.4",
                         "angular-resource":  "~1.7.4",
                         "angular-sanitize":  "~1.7.4",
                         "angular-simple-cache":  "1.0.6",
                         "angular-touch":  "~1.7.4",
                         "angular-ui-mask":  "1.8.7",
                         "angular-ui-select":  "0.19.8",
                         "bootstrap-sass-official":  "3.3.7",
                         "ng-idle":  "1.3.2",
                         "ng-notify":  "0.8.0",
                         "vkbeautify":  "*",
                         "ngclipboard":  "~2.0.0",
                         "angular-confirm":  "angular1-confirm#1.1.0",
                         "angular-ui-router":  "~1.0.20",
                         "font-awesome":  "^5.6.1",
                         "ng-confirm":  "angular-confirm#^1.1.0"
                     },
    "devDependencies":  {

                        },
    "appPath":  "src",
    "moduleName":  "sapphire",
    "overrides":  {
                      "bootstrap":  {
                                        "main":  "less/bootstrap.less dist/css/bootstrap.css dist/js/bootstrap.js"
                                    }
                  },
    "resolutions":  {
                        "angular":  "~1.7.4",
                        "angular-animate":  "~1.7.4",
                        "angular-cookies":  "~1.7.4",
                        "angular-mocks":  "~1.7.4",
                        "angular-resource":  "~1.7.4",
                        "angular-sanitize":  "~1.7.4",
                        "angular-touch":  "~1.7.4",
                        "components-font-awesome":  "~5.0.6",
                        "ngclipboard":  "~2.0.0",
                        "clipboard":  "^2.0.0",
                        "angular-ui-router":  "~1.0.20"
                    }
}

请注意version的新值... [咧嘴]

答案 1 :(得分:0)

要回答您的正则表达式方法:

字符串本身的Replace()方法用于简单替换。 PowerShell不能在那里使用正则表达式。

一种方法是使用-replace运算符,该运算符可以使用正则表达式进行高级替换。

这里是一个例子:

$data = @'
{
  "name": "sapphire",
  "version": "2.4.5",
  "dependencies": {
    "angular": "~1.7.4",
    "angular-animate": "~1.7.4",
    "angular-barcode": "0.0.4",
    "angular-bootstrap": "2.5.0",
    "angular-cookies": "~1.7.4",
    "angular-google-maps": "2.4.1",
    "angular-loading-bar": "0.9.0",
    "angular-mocks": "~1.7.4",
    "angular-resource": "~1.7.4",
    "angular-sanitize": "~1.7.4",
    "angular-simple-cache": "1.0.6",
    "angular-touch": "~1.7.4",
    "angular-ui-mask": "1.8.7",
    "angular-ui-select": "0.19.8",
    "bootstrap-sass-official": "3.3.7",
    "ng-idle": "1.3.2",
    "ng-notify": "0.8.0",
    "vkbeautify": "*",
    "ngclipboard": "~2.0.0",
    "angular-confirm": "angular1-confirm#1.1.0",
    "angular-ui-router": "~1.0.20",
    "font-awesome": "^5.6.1",
    "ng-confirm": "angular-confirm#^1.1.0"
  },
  "devDependencies": {},
  "appPath": "src",
  "moduleName": "sapphire",
  "overrides": {
    "bootstrap": {
      "main": [
        "less/bootstrap.less",
        "dist/css/bootstrap.css",
        "dist/js/bootstrap.js"
      ]
    }
  },
  "resolutions": {
    "angular": "~1.7.4",
    "angular-animate": "~1.7.4",
    "angular-cookies": "~1.7.4",
    "angular-mocks": "~1.7.4",
    "angular-resource": "~1.7.4",
    "angular-sanitize": "~1.7.4",
    "angular-touch": "~1.7.4",
    "components-font-awesome": "~5.0.6",
    "ngclipboard": "~2.0.0",
    "clipboard": "^2.0.0",
    "angular-ui-router": "~1.0.20"
  }
}
'@

$rex = '(?<="version": ")(.*)(?=",)'

$data -replace $rex, "5.1" 

正则表达式在"version": "",之间得到任何东西。因此,替换运算符将使用5.1更改与此模式匹配的所有值。 结果应该是这样的:

"version": "5.1",