首页 > 解决方案 > Powershell:在txt文件的每2行插入一个换行符

问题描述

我必须将 powershell 中的数组中的值通过管道传输到 txt 文件,但问题是这些值在没有换行符的情况下输入到文本文件中,如下所示:

价值11 价值12 价值
21
价值
22

我希望它看起来像这样:

价值
11 价值 12

价值
21 价值 22

基本上在每两行之后用换行符分隔文本。如何使用 powershell 实现这一目标?

标签: powershell

解决方案


用于Get-Content -ReadCount 2一次读取文件 2 行,然后在每行之后注入一个空字符串,然后写入一个新文件:

Get-Content .\path\to\file.txt -ReadCount 2 |ForEach-Object {
    $_  # This will expand to two lines at a time
    ''  # followed by this empty string
} |Set-Content .\path\to\output.txt

推荐阅读