简体   繁体   English

使用powershell脚本替换单词

[英]replace the word using powershell script

I have a text file for setting up environment variables. 我有一个用于设置环境变量的文本文件。 I need to change the value for 'config' variable. 我需要更改'config'变量的值。 below is the file content 以下是文件内容

env.mode: on env.mode:on
config: env config:env
server: 2 服务器:2
db : mssql db:mssql
......... .........
......... .........

env value can be anything. env值可以是任何东西。 it should be replaced to prod. 它应该被替换为prod。 (config: prod). (config:prod)。 I know we can replace the word using powershell get-content. 我知道我们可以用powershell get-content替换这个词。 since word is not static, need to set the value using Powershell. 由于word不是静态的,需要使用Powershell设置值。 Can someone help me? 有人能帮我吗?

Try this: 尝试这个:

$file = 'c:\myconfig.config'
$configValue = 'prod'
(Get-Content $file) -replace '^config:.*', "config: $configValue" | Set-Content $file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM