使用 Windows 批处理文件在文本文件中添加新行
我有一个文本文件,其中有 200 多行,我只想在第 4 行之前添加一个新行。我使用的是 Windows XP。
输入前的示例文本文件:
header 1
header 2
header 3
details 1
details 2
输出后:
header 1
header 2
header 3
<----- This is new line ---->
details 1
details 2
I have a text file which has more than 200 lines in it, and I just want to add a new line before line 4. I'm using Windows XP.
Example text file before input:
header 1
header 2
header 3
details 1
details 2
After output:
header 1
header 2
header 3
<----- This is new line ---->
details 1
details 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信你正在使用该
功能?
如果是这样,答案就是简单地添加一个“。” (点)直接在回声之后,没有其他任何东西。
例子:
I believe you are using the
function?
If so the answer would be simply adding a "." (Dot) directly after the echo with nothing else there.
Example:
免责声明:以下解决方案不会保留尾随制表符。
如果您知道文本文件中的确切行数,请尝试以下方法:
循环从原始文件中逐行读取行并将其输出。输出被重定向到临时文件。当到达某一行时,在其前面输出一个空行。
完成后,原始文件将被删除,临时文件将被指定为原始名称。
UPDATE
如果事先未知行数,可以使用以下方法获取:(
此行仅替换上述脚本中的
SET Totallines=200
行。 )该方法有一个小缺陷:如果文件以空行结尾,则结果将是实际行数减一。如果您需要解决方法(或者只是想安全起见),可以使用 这个答案。
DISCLAIMER: The below solution does not preserve trailing tabs.
If you know the exact number of lines in the text file, try the following method:
The loop reads lines from the original file one by one and outputs them. The output is redirected to a temporary file. When a certain line is reached, an empty line is output before it.
After finishing, the original file is deleted and the temporary one gets assigned the original name.
UPDATE
If the number of lines is unknown beforehand, you can use the following method to obtain it:
(This line simply replaces the
SET totallines=200
line in the above script.)The method has one tiny flaw: if the file ends with an empty line, the result will be the actual number of lines minus one. If you need a workaround (or just want to play safe), you can use the method described in this answer.
您可以使用:
或类似的东西:
You can use:
or something like this:
假设您要插入特定的文本行(不是空行):
Suppose you want to insert a particular line of text (not an empty line):