如何在没有 header 的情况下从文本文件 select 前两列按制表符拆分

How to select first two columns split by tab from a text file without header in Powershell

示例数据如下(按制表符拆分):

value1  value2  value3  value4

我用了get-content | format-table 获取数据,但不知道如何获取前两列(value1 和 value2),因为它们不是 headers 来执行 select.

谢谢,

您可以使用 Import-Csv-Header 参数来自己定义 headers:

Import-Csv .\values.txt -Delimiter "`t" -Header col1,col2,col3,col4 |Format-Table col1,col2

如果要使用Get-Content,可以使用-split运算符:

Get-Content .\values.txt |ForEach-Object {
    $col1,$col2,$null = $_ -split '\s+'
    New-Object psobject -Property @{
        Col1 = $col1
        Col2 = $col2
    }
}

\s+ 是“1 个或多个空白字符”的正则表达式