Powershell,如何从文件中获取所需的列

问题描述 投票:1回答:2

在Windows Powershell中输入文件数据

WL363998 google.com 172.217.166.46 32 7 WL363998 fb.com 157.240.16.35 32 7 WL363998 bing.com 13.107.21.200 32 6

我需要的输出文件

google.com 172.217.166.46 fb.com 157.240.16.35 bing.com 13.107.21.200

powershell file-io
2个回答
1
投票

您可以尝试这样的事情:

(& {
    foreach ($line in Get-Content -Path .\data.txt)
    {
        $columns = $line -split '\s+'
        [PSCustomObject]@{
            Domain = $columns[1]
            IP = $columns[2]
        }
    } 
} | Out-String).Trim() | Out-File -FilePath output.txt -NoNewline

说明:

  • Get-Content的每一行读入字符串数组。
  • Get-Content在所有空白处分割每一行。有关更多信息,请参见\s+
  • 将第一和第二列插入about_split
  • 管道到about_split,因此我们可以PSCustomObject尾随空白。
  • 将管道插入PSCustomObject以创建新的输出文件,请确保我们不在Out-String中添加新行(可选)。

output.txt

Out-String

0
投票

尝试一下:

Trim()
© www.soinside.com 2019 - 2024. All rights reserved.