I am really new to powershell. I need to replace a value (that is always changing) in a file. The value looks like this:
"version": "2.4.5",
And I wish to replace the number. I have this bit of code:
(Get-Content .\bower.json).replace('2.4.5', $version) | Set-Content .\bower.json
But that requires that the version number is always 2.4.5
before setting it to the new version. As you can figure out, the next time around that number won't be the same.
Does anyone know of a way I can replace the version number regardless of what it is? Maybe with regex?
After a bit of playing, I have tried this:
$pattern = '\"version\": \"(.*)\"'
$replace = """version"": ""$version""";
(Get-Content .\bower.json).replace($pattern, $replace) | Set-Content .\bower.json
But it doesn't replace anything :( If I run:
$replace -match $pattern
it returns "True", so I would expect it to replace correctly? This is my bower.json file:
{
"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"
}
}