如何在 PowerShell 中连接两个文本文件?
我正在尝试在 Unix 中复制 cat
命令的功能。
我想避免将两个文件显式读入变量,将变量连接在一起,然后写出连接的变量的解决方案。
I am trying to replicate the functionality of the cat
command in Unix.
I would like to avoid solutions where I explicitly read both files into variables, concatenate the variables together, and then write out the concatenated variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
只需使用
Get-Content
和Set-Content
cmdlet:您可以连接多个也有两个具有这种风格的文件。
如果源文件的名称类似,则可以使用通配符:
注 1: PowerShell 5 及更早版本允许使用别名
cat
和更简洁地完成此操作sc
分别用于Get-Content
和Set-Content
。然而,这些别名是有问题的,因为cat
是 *nix 系统中的系统命令,而sc
是 Windows 系统中的系统命令 - 因此不建议使用它们,并且在事实上,从 PowerShell Core (v7) 开始,sc
甚至不再定义。 PowerShell 团队建议一般情况下不要使用别名。注意 2:请小心使用通配符 - 如果您尝试输出到
inputFiles.txt
(或与模式匹配的类似内容),PowerShell 将陷入无限循环! (我刚刚对此进行了测试。)注3:输出到带有
>
的文件不会保留字符编码!这就是为什么建议使用Set-Content
。Simply use the
Get-Content
andSet-Content
cmdlets:You can concatenate more than two files with this style, too.
If the source files are named similarly, you can use wildcards:
Note 1: PowerShell 5 and older versions allowed this to be done more concisely using the aliases
cat
andsc
forGet-Content
andSet-Content
respectively. However, these aliases are problematic becausecat
is a system command in *nix systems, andsc
is a system command in Windows systems - therefore using them is not recommended, and in factsc
is no longer even defined as of PowerShell Core (v7). The PowerShell team recommends against using aliases in general.Note 2: Be careful with wildcards - if you try to output to
inputFiles.txt
(or similar that matches the pattern), PowerShell will get into an infinite loop! (I just tested this.)Note 3: Outputting to a file with
>
does not preserve character encoding! This is why usingSet-Content
is recommended.不要使用
>
;它搞乱了字符编码。使用:Do not use
>
; it messes up the character encoding. Use:在
cmd
中,您可以执行以下操作:在 PowerShell 中,这将是:
而 PowerShell 方式是使用 gc,上面的速度会相当快,特别是对于大文件。它也可以使用
/BASCII 文件上使用代码>开关。
In
cmd
, you can do this:In PowerShell this would be:
While the PowerShell way would be to use gc, the above will be pretty fast, especially for large files. And it can be used on on non-ASCII files too using the
/B
switch.您可以使用 Add-Content cmdlet。也许它比其他解决方案快一点,因为我不检索第一个文件的内容。
You could use the Add-Content cmdlet. Maybe it is a little faster than the other solutions, because I don't retrieve the content of the first file.
要在命令提示符中连接文件,PowerShell 会将
type
命令转换为Get-Content
,这意味着使用type
时会出现错误> PowerShell 中的命令,因为Get-Content
命令需要用逗号分隔文件。 PowerShell 中的相同命令是To concat files in command prompt it would be
PowerShell converts the
type
command toGet-Content
, which means you will get an error when using thetype
command in PowerShell because theGet-Content
command requires a comma separating the files. The same command in PowerShell would be要保留编码和行结尾:
注意:AFAIR,旧的 Powershell 不支持其参数(<3?<4?)
To keep encoding and line endings:
Note: AFAIR, whose parameters aren't supported by old Powershells (<3? <4?)
我用过:
这个附加罚款。我添加了 ASCII 编码来删除 Notepad++ 在没有显式编码的情况下显示的空字符。
I used:
This appended fine. I added the ASCII encoding to remove the nul characters Notepad++ was showing without the explicit encoding.
如果您需要按特定参数(例如日期时间)对文件进行排序:
If you need to order the files by specific parameter (e.g. date time):
您可以执行以下操作:
其中
>
是“out-file”的别名,并且 >>是“out-file -append”的别名。You can do something like:
Where
>
is an alias for "out-file", and >> is an alias for "out-file -append".由于大多数其他回复经常出现格式错误(由于管道),最安全的做法如下:
我知道您想避免将 $SomeAdditionalFile 的内容读入变量,但为了保存例如你的换行格式我认为没有正确的方法可以做到这一点。
解决方法是逐行循环遍历 $SomeAdditionalFile 并将其通过管道传输到 $YourMasterFile 中。然而,这过于消耗资源。
Since most of the other replies often get the formatting wrong (due to the piping), the safest thing to do is as follows:
I know you wanted to avoid reading the content of $SomeAdditionalFile into a variable, but in order to save for example your newline formatting i do not think there is proper way to do it without.
A workaround would be to loop through your $SomeAdditionalFile line by line and piping that into your $YourMasterFile. However this is overly resource intensive.
我认为“powershell方式”可能是:
I think the "powershell way" could be :