powershell - 如何替换特定的行号

标签 powershell

我正在尝试使用PowerShell替换txt文件中的第11行。

首先,我尝试替换一个特定的单词,但是更改太多:

$output= (Resolve-DnsName -name name1).IPAddress 

(Get-Content "C:\test\test.txt") -replace "IPADDRESS=","IPADDRESS=$output"  | Set-Content C:\test\test.txt

最佳答案

如果要替换特定行中的某些内容,可以在Get-Content cmdlet返回的字符串数组上使用索引运算符:

$content = Get-Content "C:\test\test.txt"
$content[10] = -replace "IPADDRESS=","IPADDRESS=$output" 
$content | Set-Content C:\test\test.txt

关于powershell - 如何替换特定的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43519871/

相关文章:

powershell - Chocolatey包中的可选依赖项

regex - 如何匹配字符串,除非它包含某些内容?

powershell - 通过Powershell运行远程进程

powershell - 使用 Powershell 复制时跳过隐藏文件

git - 在 git 中创建别名

powershell - 术语 'xWebsite' 未被识别为 cmdlet 的名称

powershell - 为什么 Windows 远程管理服务坚持为 "delayed start"?

azure - 使用 powershell 将 docker 镜像推送到 azure 容器注册表存储库

powershell - 大文本文件的匹配操作速度问题

powershell - 使用PowerShell与Code::Blocks