PowerShell:如何从分隔文本输入中删除列?

发布于 2024-11-29 21:57:51 字数 469 浏览 0 评论 0原文

我有一个文本文件,其中包含 5 列由空格分隔的文本。例如:

10 45 5 23 78
89 3 56 12 56
999 4 67 93 5

使用PowerShell,如何删除最右边的两列?生成的文件应该是:

10 45 5
89 3 56
999 4 67

我可以使用 -split 运算符提取各个项目。但是,这些项目出现在不同的行上,我不知道如何将它们恢复为每行 3 个项目。

并使问题更加通用(并对其他人有帮助):How to use PowerShell to remove the data at multiple columns in the range [0,n-1] 鉴于输入包含带分隔符的行每n 列的数据?

I have a text file with 5 columns of text delimited by whitespace. For example:

10 45 5 23 78
89 3 56 12 56
999 4 67 93 5

Using PowerShell, how do I remove the rightmost two columns? The resulting file should be:

10 45 5
89 3 56
999 4 67

I can extract the individual items using the -split operator. But, the items appear on different lines and I do not see how I can get them back as 3 items per line.

And to make the question more generic (and helpful to others): How to use PowerShell to remove the data at multiple columns in the range [0,n-1] given an input that has lines with delimited data of n columns each?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

烟燃烟灭 2024-12-06 21:57:51

读取文件内容,将其转换为 csv 并仅选择前 3 列:

Import-Csv .\file.txt -Header col1,col2,col3,col4,col5 -Delimiter ' ' | Select-Object col1,col2,col3

如果您只想要值(不带标题):

Import-Csv .\file.txt -Header col1,col2,col3,col4,col5 -Delimiter ' ' | Select-Object col1,col2,col3 | Format-Table -HideTableHeaders -AutoSize

将结果保存回文件:

(Import-Csv .\file.txt -Header col1,col2,col3,col4,col5 -Delimiter ' ') | Foreach-Object { "{0} {1} {2}" -f $_.col1,$_.col2,$_.col3} | Out-File .\file.txt

更新:

只是另一个选项:

(Get-Content .\file.txt) | Foreach-Object { $_.split()[0..2] -join ' ' } | Out-File .\file.txt

Read the file content, convert it to a csv and select just the first 3 columns:

Import-Csv .\file.txt -Header col1,col2,col3,col4,col5 -Delimiter ' ' | Select-Object col1,col2,col3

If you want just the values (without a header):

Import-Csv .\file.txt -Header col1,col2,col3,col4,col5 -Delimiter ' ' | Select-Object col1,col2,col3 | Format-Table -HideTableHeaders -AutoSize

To save back the results to the file:

(Import-Csv .\file.txt -Header col1,col2,col3,col4,col5 -Delimiter ' ') | Foreach-Object { "{0} {1} {2}" -f $_.col1,$_.col2,$_.col3} | Out-File .\file.txt

UPDATE:

Just another option:

(Get-Content .\file.txt) | Foreach-Object { $_.split()[0..2] -join ' ' } | Out-File .\file.txt
旧情别恋 2024-12-06 21:57:51

一种方法是:(

gc input.txt | %{[string]::join(" ",$_.split()[0..2]) } | out-file output.txt

用 n-1 替换 2)

One way is:

gc input.txt | %{[string]::join(" ",$_.split()[0..2]) } | out-file output.txt

(replace 2 by n-1)

沩ん囻菔务 2024-12-06 21:57:51

这是通用解决方案:

param
(
    # Input data file
    [string]$Path = 'data.txt',
    # Columns to be removed, any order, dupes are allowed
    [int[]]$Remove = (4, 3, 4, 3)
)

# sort indexes descending and remove dupes
$Remove = $Remove | Sort-Object -Unique -Descending

# read input lines
Get-Content $Path | .{process{
    # split and add to ArrayList which allows to remove items
    $list = [Collections.ArrayList]($_ -split '\s')

    # remove data at the indexes (from tail to head due to descending order)
    foreach($i in $Remove) {
        $list.RemoveAt($i)
    }

    # join and output
    $list -join ' '
}}

Here is the generic solution:

param
(
    # Input data file
    [string]$Path = 'data.txt',
    # Columns to be removed, any order, dupes are allowed
    [int[]]$Remove = (4, 3, 4, 3)
)

# sort indexes descending and remove dupes
$Remove = $Remove | Sort-Object -Unique -Descending

# read input lines
Get-Content $Path | .{process{
    # split and add to ArrayList which allows to remove items
    $list = [Collections.ArrayList]($_ -split '\s')

    # remove data at the indexes (from tail to head due to descending order)
    foreach($i in $Remove) {
        $list.RemoveAt($i)
    }

    # join and output
    $list -join ' '
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文