gpt4 book ai didi

powershell - 添加内容 - 附加到特定行

转载 作者:行者123 更新时间:2023-12-04 17:08:38 25 4
gpt4 key购买 nike

我想将文本添加到 txt 文件的特定行中。我在互联网上找不到任何解决方案。

此代码在另一行中添加文本(我想要例如第二行):

$test_out = "test"

$test_out | Add-Content "C:\tmp\test.txt"

最佳答案

如果你想在特定行的末尾添加文本,你可以这样做

$fileContent = Get-Content $filePath
$fileContent[$lineNumber-1] += $textToAdd
$fileContent | Set-Content $filePath

如果要替换文本而不是添加文本,只需删除“+”号即可。
你当然必须设置变量
首先是 $filePath、$textToAdd 和 $lineNumber。

关于powershell - 添加内容 - 附加到特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31957901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com