gpt4 book ai didi

powershell - powershell 中的 `cat somefile | cat` 给出 10000 行而不是 100 行

转载 作者:行者123 更新时间:2023-12-05 01:27:27 27 4
gpt4 key购买 nike

有人可以在这里解释我的误解吗? cat 一些文件 | cat 正在输出 10000 行而不是 100 行。我习惯了管道的 Unix 行为。这是重现问题的脚本(需要在 PowerShell 提示符下逐行输入):

seq 1 100 > somefile
cat somefile # works as expected, outputs 100 lines
cat somefile | Measure-Object # 100 lines, expected.
cat somefile | cat # OUTPUTS 10000 lines!!!
# wait did I really just see that
cat somefile | cat | Measure-Object
# 10000 lines??!!!
cat somefile | cat | cat | Measure-Object
# 57300 lines??? That's not even a pattern!

很明显,我不知道这里发生了什么。我期待 bash 行为。我知道做 cat somefile | 没有用cat,但这仍然令人惊讶。

Bash 行为是 cat somefile |猫 |猫 | cat 始终提供与 cat somefile 完全相同的内容。

最佳答案

Get-Content 的别名 cat 是出于我认为的方便原因而引入的(让 Unix 用户对 PowerShell 感觉更舒服)。不过,这并不会使 Get-Content 的行为与 Unix 命令 cat 完全相同。该 cmdlet 特别不会回显来自管道的字符串输入:

PS C:\Temp> 'foo' | catcat : The input object cannot be bound to any parameters for the command eitherbecause the command does not take pipeline input or the input and its propertiesdo not match any of the parameters that take pipeline input.At line:1 char:9+ 'foo' | cat+         ~~~

这就是 Write-Output(或其别名 echo)的用途:

PS C:\Temp> 'foo' | Write-Outputfoo

接受管道输入的Get-Content的默认参数分别是-Path-LiteralPath,它们都需要一个有效的路径.

在仔细检查 Get-Content 的输出后,您会注意到这些对象不仅具有字符串对象的通常属性,而且还具有一些包含数据来源文件信息的属性已读取,特别是 PSPath:

PS C:\Temp> 4..6 > out.txtPS C:\Temp> cat .\out.txt456PS C:\Temp> cat .\out.txt | Get-Member   TypeName: System.StringName             MemberType            Definition----             ----------            ----------...PSChildName      NoteProperty          string PSChildName=out.txtPSDrive          NoteProperty          PSDriveInfo PSDrive=CPSParentPath     NoteProperty          string PSParentPath=C:\TempPSPath NoteProperty string PSPath=C:\Temp\out.txtPSProvider       NoteProperty          ProviderInfo PSProvider=Microsoft.PowerShell.Core\FileSystemReadCount        NoteProperty          long ReadCount=1Chars            ParameterizedProperty char Chars(int index) {get;}Length           Property              int Length {get;}

PSPath 属性用作第二个 Get-Content 的输入,导致输入文件中的每一行触发另一个 Get-Content 相同的文件。但是,第一个 Get-Content 的输出对象也有一个属性 ReadCount(表示已经从文件中读取的行数),这也会发生成为 Get-Content 的参数。因此,第二个 Get-Content 读取输入文件的方式与第一个不同完全ReadCount=2 一次读取文件中的 2 行,ReadCount=3 一次读取 3 行,依此类推。

PS C:\Temp> cat .\out.txt | cat4   # ← input line 1 ("4"), ReadCount = 1, 1st read (returns "4")5   # ← input line 1 ("4"), ReadCount = 1, 2nd read (returns "5")6   # ← input line 1 ("4"), ReadCount = 1, 3rd read (returns "6")4   # ← input line 2 ("5"), ReadCount = 2, 1st read (returns "4", "5")56   # ← input line 2 ("5"), ReadCount = 2, 2nd read (returns "6")4   # ← input line 3 ("6"), ReadCount = 3, 1st read (returns "4", "5", "6")56

因为额外的管道步骤 (cat .\out.txt | cat | cat ...) 不会产生 nm 输出行 ( n 是文件中的行数,m 是流水线步骤数)。

关于powershell - powershell 中的 `cat somefile | cat` 给出 10000 行而不是 100 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54544355/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com