在 PS 脚本中格式化 ForEach-Object 输出

发布于 2025-01-10 21:20:25 字数 1925 浏览 0 评论 0原文

我正在尝试从 PowerShell 脚本中的 CSV 文件输出数据(RUSH 的歌曲和专辑列表),并且我按照我需要的方式对数据进行了排序,但我不知道如何将其格式化我需要的方式。以下是我的代码当前的编写方式:

    $output = Import-CSV $DatabasePath -Delimiter "`t" | Sort-Object @{Expression= 'Year'; Descending = $true}, @{Expression='Song'; Ascending=$true}
    #$output
    $output | ForEach-Object {
        Write-Host "$_.Album "($_.Year)" "`n""
        Write-Host "$tab $_.Song"

这就是它的输出方式:


         @{Song=The Story so Far; Album=R40 Live; Year=2015; Notes=Drum solo; interlude during Cygnus X-1}.Song
@{Song=Drumbastica; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo; interlude during Headlong Flight}.Album  2013   

         @{Song=Drumbastica; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo; interlude during Headlong Flight}.Song 
@{Song=Here It Is!; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo}.Album  2013

         @{Song=Here It Is!; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo}.Song
@{Song=Peke's Repose; Album=Clockwork Angels Tour; Year=2013; Notes=Guitar solo; lead-in to Halo Effect}.Album  2013

我需要输出像这样显示:

R40 Live (2015)
    The Story so Far
Clockwork Angels Tour (2013)
    Drumbastica
    Here It Is!
    Peke's Repose
    The Percussor
Clockwork Angels (2012)
    BU2B
...

(注意:请忽略上面示例中的颜色。) 使用循环从 CSV 文件获取数据后,我想做的就是在第一行的括号中显示专辑名称和年份,然后将与该专辑相关的所有歌曲按字母顺序标记在专辑名称后面的行。我该怎么做?

编辑:按照@BrettFord1999建议,我得到的输出显示如下:

R40 Live (2015)  
         The Story so Far
Clockwork Angels Tour (2013)
         Drumbastica
Clockwork Angels Tour (2013)
         Here It Is!
Clockwork Angels Tour (2013)
         Peke's Repose
Clockwork Angels Tour (2013)
         The Percussor
Clockwork Angels (2012)
         BU2B

但是,我只想显示专辑一次而不是多次。有没有办法显示每个专辑名称一次,而不是显示它链接到的每首歌曲?

我会提供原始文件,但我不知道如何在这里分享它。我对 StackOverflow 还很陌生。

I'm trying to output data (a list of songs and albums by RUSH) from a CSV file in my PowerShell script, and I got the data sorted the way I need it, but I can't figure out how to format it the way I need. The following is how my code is currently written:

    $output = Import-CSV $DatabasePath -Delimiter "`t" | Sort-Object @{Expression= 'Year'; Descending = $true}, @{Expression='Song'; Ascending=$true}
    #$output
    $output | ForEach-Object {
        Write-Host "$_.Album "($_.Year)" "`n""
        Write-Host "$tab $_.Song"

and this is how it outputs:


         @{Song=The Story so Far; Album=R40 Live; Year=2015; Notes=Drum solo; interlude during Cygnus X-1}.Song
@{Song=Drumbastica; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo; interlude during Headlong Flight}.Album  2013   

         @{Song=Drumbastica; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo; interlude during Headlong Flight}.Song 
@{Song=Here It Is!; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo}.Album  2013

         @{Song=Here It Is!; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo}.Song
@{Song=Peke's Repose; Album=Clockwork Angels Tour; Year=2013; Notes=Guitar solo; lead-in to Halo Effect}.Album  2013

I need the output to display like this:

R40 Live (2015)
    The Story so Far
Clockwork Angels Tour (2013)
    Drumbastica
    Here It Is!
    Peke's Repose
    The Percussor
Clockwork Angels (2012)
    BU2B
...

(NOTE: please ignore the color in the example above.)
What I'm trying to do after getting the data from the CSV file with the loop is to display the Album name with the Year in paratheses on the first line, then all the songs pertaining to that album tabbed in and in alphabetical order on the lines following the album name. How can I do this?

Edit: Following @BrettFord1999 Suggestions, I got the output to display like this:

R40 Live (2015)  
         The Story so Far
Clockwork Angels Tour (2013)
         Drumbastica
Clockwork Angels Tour (2013)
         Here It Is!
Clockwork Angels Tour (2013)
         Peke's Repose
Clockwork Angels Tour (2013)
         The Percussor
Clockwork Angels (2012)
         BU2B

However, I only want to display the Album once and not multiple times. Is there a way to display each album name once instead of with every song it's linked to?

I would provide the original file except I can't figure out how to share it on here. I'm still new to StackOverflow.

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

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

发布评论

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

评论(2

浮生面具三千个 2025-01-17 21:20:25

我首先尝试将值设置为变量,然后针对输出操作它。

如果您可以提供 CSV 的副本,我可以进一步查看。

  $output = Import-CSV $DatabasePath -Delimiter "`t" | Sort-Object @{Expression= 'Year'; Descending = $true}, @{Expression='Song'; Ascending=$true}
    #$output
    $output | ForEach-Object {
        $album = $_.Album
        $year = $_.Year
        $year = $_.Song

        Write-Host "$Album ($Year)"
        etc...
        }

I would first try setting the values to a variable then manipulating it for your output.

If you could provide a copy of the CSV I could take a further look.

  $output = Import-CSV $DatabasePath -Delimiter "`t" | Sort-Object @{Expression= 'Year'; Descending = $true}, @{Expression='Song'; Ascending=$true}
    #$output
    $output | ForEach-Object {
        $album = $_.Album
        $year = $_.Year
        $year = $_.Song

        Write-Host "$Album ($Year)"
        etc...
        }
奢华的一滴泪 2025-01-17 21:20:25

它比我预期的要复杂一些,因为如果用户输入文件路径,我还必须设置将格式化的歌曲列表输出到文件的功能,但代码现在输出如上所示的歌曲列表。这是完成的代码:

$prop1 =@{Expression= 'Year'; Descending = $true}
$prop2 =@{Expression='Album'; Ascending=$true}
$prop3 =@{Expression='Song'; Ascending=$true}

$tab = [char]9
$output = Import-CSV $DatabasePath -Delimiter "`t" | Sort-Object $prop1, $prop2, $prop3

    $test = @()
    $count = 0
    $output | ForEach-Object {
        $album = $_.Album
        $year = $_.Year
        $song = $_.Song
        if ($output[$count].album -like $output[$count -1].album){
           $test += "$tab$Song"
        } else{
            $test += "$Album ($Year)"
            $test += "$tab$Song"
        } $count++

    }  
    if ($path.Length -gt 0){
        $test | Out-File -Path $Path
    } else {
       $test | ForEach-Object {
           Write-Host $_
       }
    }

It got a little more complicated than I anticipated because I had to also setup the function to output the formatted song list to file if the user input a file path, but the code now outputs the song list as displayed above. Here is the completed code:

$prop1 =@{Expression= 'Year'; Descending = $true}
$prop2 =@{Expression='Album'; Ascending=$true}
$prop3 =@{Expression='Song'; Ascending=$true}

$tab = [char]9
$output = Import-CSV $DatabasePath -Delimiter "`t" | Sort-Object $prop1, $prop2, $prop3

    $test = @()
    $count = 0
    $output | ForEach-Object {
        $album = $_.Album
        $year = $_.Year
        $song = $_.Song
        if ($output[$count].album -like $output[$count -1].album){
           $test += "$tab$Song"
        } else{
            $test += "$Album ($Year)"
            $test += "$tab$Song"
        } $count++

    }  
    if ($path.Length -gt 0){
        $test | Out-File -Path $Path
    } else {
       $test | ForEach-Object {
           Write-Host $_
       }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文