我正在尝试使用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/