在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
您可以尝试这样的事情:
(& {
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
尝试一下:
Trim()