powershell - Powershell 搜索特定字符串,然后在其后添加文本
问题描述
我很确定这很容易做到,但我找不到答案。假设我有一个包含一堆行的文本文件。下面的文本文件的内容
dbuser=admin
dbpassword=
所以在Powershell中我想找到字符串“dbpassword =”并在=之后添加文本“password”。我搜索过的解决方案有 -replace 但我不想替换该行,只想向其中添加“密码”。
任何人都可以分享 Powershell 代码可能是什么吗?
解决方案
感谢大家的贡献,我最终找到了这个解决方案。
$config = Get-Content path\to\file\somefile.txt
$password = abcdef
$config -replace "dbpassword=.*",("dbpassword="+$password) |
Set-Content path\to\file\somefile.txt
推荐阅读
- ruby-on-rails - Rails index.html.erb 删除项目 - 不工作
- regex - Oracle SQL:提取每行中的所有匹配字符串
- asp.net - 访问嵌套 UpdatePanel 内的模型绑定项
- linux - Raspbian 启动时自动运行命令
- algorithm - 寻找最长路径网格
- reactjs - 处理 ag grid react 并渲染复选框网格
- google-cloud-datastore - Most efficient way to delete thousands of entities in datastore
- postgresql - Postgresql如何在事务中使用多个存储过程
- pointers - 如果对象在切片中,方法不会改变对象的值
- asp.net-web-api - 在 Dynamics CRM 中使用 C# 和 WEB API 批量插入