如何在 PowerShell 中连接两个文本文件?

发布于 2024-12-25 01:12:06 字数 95 浏览 2 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

油焖大侠 2025-01-01 01:12:06

只需使用 Get-ContentSet-Content cmdlet:

Get-Content inputFile1.txt, inputFile2.txt | Set-Content joinedFile.txt

您可以连接多个也有两个具有这种风格的文件。

如果源文件的名称类似,则可以使用通配符:

Get-Content inputFile*.txt | Set-Content joinedFile.txt

注 1: PowerShell 5 及更早版本允许使用别名 cat更简洁地完成此操作sc 分别用于 Get-ContentSet-Content。然而,这些别名是有问题的,因为 cat 是 *nix 系统中的系统命令,而 sc 是 Windows 系统中的系统命令 - 因此不建议使用它们,并且在事实上,从 PowerShell Core (v7) 开始,sc 甚至不再定义。 PowerShell 团队建议一般情况下不要使用别名

注意 2:请小心使用通配符 - 如果您尝试输出到 inputFiles.txt (或与模式匹配的类似内容),PowerShell 将陷入无限循环! (我刚刚对此进行了测试。)

注3:输出到带有>的文件不会保留字符编码!这就是为什么建议使用Set-Content

Simply use the Get-Content and Set-Content cmdlets:

Get-Content inputFile1.txt, inputFile2.txt | Set-Content joinedFile.txt

You can concatenate more than two files with this style, too.

If the source files are named similarly, you can use wildcards:

Get-Content inputFile*.txt | Set-Content joinedFile.txt

Note 1: PowerShell 5 and older versions allowed this to be done more concisely using the aliases cat and sc for Get-Content and Set-Content respectively. However, these aliases are problematic because cat is a system command in *nix systems, and sc is a system command in Windows systems - therefore using them is not recommended, and in fact sc 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 using Set-Content is recommended.

愁以何悠 2025-01-01 01:12:06

不要使用>;它搞乱了字符编码。使用:

Get-Content files.* | Set-Content newfile.file

Do not use >; it messes up the character encoding. Use:

Get-Content files.* | Set-Content newfile.file
许一世地老天荒 2025-01-01 01:12:06

cmd 中,您可以执行以下操作:

copy one.txt+two.txt+three.txt four.txt

在 PowerShell 中,这将是:

cmd /c copy one.txt+two.txt+three.txt four.txt

而 PowerShell 方式是使用 gc,上面的速度会相当快,特别是对于大文件。它也可以使用 /BASCII 文件上使用代码>开关。

In cmd, you can do this:

copy one.txt+two.txt+three.txt four.txt

In PowerShell this would be:

cmd /c copy one.txt+two.txt+three.txt four.txt

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.

小嗷兮 2025-01-01 01:12:06

您可以使用 Add-Content cmdlet。也许它比其他解决方案快一点,因为我不检索第一个文件的内容。

gc .\file2.txt| Add-Content -Path .\file1.txt

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.

gc .\file2.txt| Add-Content -Path .\file1.txt
单身情人 2025-01-01 01:12:06

要在命令提示符中连接文件,PowerShell 会将

type file1.txt file2.txt file3.txt > files.txt

type 命令转换为 Get-Content,这意味着使用 type 时会出现错误> PowerShell 中的命令,因为 Get-Content 命令需要用逗号分隔文件。 PowerShell 中的相同命令是

Get-Content file1.txt,file2.txt,file3.txt | Set-Content files.txt

To concat files in command prompt it would be

type file1.txt file2.txt file3.txt > files.txt

PowerShell converts the type command to Get-Content, which means you will get an error when using the type command in PowerShell because the Get-Content command requires a comma separating the files. The same command in PowerShell would be

Get-Content file1.txt,file2.txt,file3.txt | Set-Content files.txt
弄潮 2025-01-01 01:12:06

要保留编码和行结尾:

Get-Content files.* -Raw | Set-Content newfile.file -NoNewline

注意:AFAIR,旧的 Powershell 不支持其参数(<3?<4?)

To keep encoding and line endings:

Get-Content files.* -Raw | Set-Content newfile.file -NoNewline

Note: AFAIR, whose parameters aren't supported by old Powershells (<3? <4?)

仅此而已 2025-01-01 01:12:06

我用过:

Get-Content c:\FileToAppend_*.log | Out-File -FilePath C:\DestinationFile.log 
-Encoding ASCII -Append

这个附加罚款。我添加了 ASCII 编码来删除 Notepad++ 在没有显式编码的情况下显示的空字符。

I used:

Get-Content c:\FileToAppend_*.log | Out-File -FilePath C:\DestinationFile.log 
-Encoding ASCII -Append

This appended fine. I added the ASCII encoding to remove the nul characters Notepad++ was showing without the explicit encoding.

梦年海沫深 2025-01-01 01:12:06

如果您需要按特定参数(例如日期时间)对文件进行排序:

gci *.log | sort LastWriteTime | % {$(Get-Content $_)} | Set-Content result.log

If you need to order the files by specific parameter (e.g. date time):

gci *.log | sort LastWriteTime | % {$(Get-Content $_)} | Set-Content result.log
俏︾媚 2025-01-01 01:12:06

您可以执行以下操作:

get-content input_file1 > output_file
get-content input_file2 >> output_file

其中 > 是“out-file”的别名,并且 >>是“out-file -append”的别名。

You can do something like:

get-content input_file1 > output_file
get-content input_file2 >> output_file

Where > is an alias for "out-file", and >> is an alias for "out-file -append".

挽心 2025-01-01 01:12:06

由于大多数其他回复经常出现格式错误(由于管道),最安全的做法如下:

add-content $YourMasterFile -value (get-content $SomeAdditionalFile)

我知道您想避免将 $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:

add-content $YourMasterFile -value (get-content $SomeAdditionalFile)

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.

划一舟意中人 2025-01-01 01:12:06

我认为“powershell方式”可能是:

set-content destination.log -value (get-content c:\FileToAppend_*.log )

I think the "powershell way" could be :

set-content destination.log -value (get-content c:\FileToAppend_*.log )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文