添加内容 - 附加到特定行 [英] Add-Content - append to specific line

查看:39
本文介绍了添加内容 - 附加到特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add text into specific line of txt file. I can't find any solution on internet.

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

This code adds text in further line (I want for example second line):

$test_out = "test"

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

推荐答案

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

If you want to add text to the end of a specific line, you can do it like this

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

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

If you want to replace the text instead of adding, just remove the '+' sign. You do of course have to set the variables $filePath, $textToAdd and $lineNumber first.

这篇关于添加内容 - 附加到特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆