【问题标题】:Using powershell to collect certain columns of data使用 powershell 收集某些数据列
【发布时间】:2015-05-19 01:53:30
【问题描述】:

我在让 windows powershell 做我想做的事情时遇到了一些问题。基本上,我的当前文件夹中有 24 个子文件夹,分别命名为 Angle1、Angle2 等,直到 Angle 24。

在每个子文件夹中都有一个名为 output.txt 的文件。在 output.txt 中,有 10 列数据,以空格分隔。我想在每个子文件夹中获取每个 output.txt 文件的第 9 列,并将它们全部放在父文件夹中名为 total.txt 的新文件中。所以,最终,我将有 24 列数据,每列用空格分隔。 total.txt 的第一列对应 Angle1 中 output.txt 的第 9 列,以此类推

请注意,Angle 文件夹还有其他我想忽略的子文件夹。

尽管尝试了很多次,但我还是无法让它发挥作用。我不确定是否需要使用 -Recurse 选项来执行此操作,或者 Powershell 是否甚至能够执行此操作。这里有没有人是powershell专家,可以说这是否可能?

一个小的除了来自一个 output.txt 文件:

1348 2 26.0785 3.18115 20.5328 -0.79613 3.18115 2.51545 4.13292 0.278888 
1348 2 26.8091 2.07125 19.4069 -0.0655152 2.07125 1.38956 2.49505 0.254024 
1348 2 24.162 3.90054 20.4261 -2.71258 3.90054 2.40877 5.32677 0.0208912 
1348 2 24.1527 3.90493 20.4233 -2.72189 3.90493 2.40595 5.33346 0.00646916 
1348 2 24.1436 3.88005 20.4429 -2.73095 3.88005 2.42555 5.32881 0.0127918 
1348 2 24.1087 3.87723 20.4519 -2.76586 3.87723 2.43455 5.34882 0.0198102 
1348 2 24.2572 3.83432 20.4136 -2.61737 3.83432 2.39624 5.22442 0.0243901 
1348 2 24.1609 3.7174 19.8739 -2.71375 3.7174 1.85659 4.9629 0.0838124

我使用的代码是在 Linux 中,在某种程度上可以工作:

awk '{print $9}' Angle1/output.txt >>tmp
for ((i=2;i<25;i++))
do
awk '{print $9}' Angle${i}/output.txt |paste -d " " tmp - >>total.txt
mv total.txt tmp
done
mv tmp total.txt

但是,现在我需要在 Powershell 中运行,而不是在 linux 中运行。目前我真的没有任何远程工作的代码。基本上是 Get-Child 命令等的集合,但没有什么能接近我想要的。

【问题讨论】:

  • 你能发布一个 output.txt 的例子和你到目前为止尝试过的代码吗?这当然可以使用 powershell。

标签: powershell


【解决方案1】:

假设所有文件的行数相同:

$Results=1..24|ForEach-Object {
    ,@(Get-Content Angle$_\output.txt|ForEach-Object {(-split$_)[8]})
}
&{
    for($i=0;$i-lt$Results[0].Length;++$i){
        @($Results|ForEach-Object {$_[$i]})-join' '
    }
}|Set-Content total.txt

【讨论】:

    【解决方案2】:

    试试这个

                $result = @()
        $Anglecount = 0
    
        do {
        $Anglecount ++
            $content = get-content (".\Angle"+$i+"\output.txt")
            foreach ($line in $content) { 
    
                $trimmed = $line.Trim()
                $split = $line.Split(" ")
                $result += $split[8]
    
            }
            $Anglecount
        } until ($Anglecount -eq 24)
    
        $result | Out-File total.txt
    

    不是最优雅的解决方案,但会为您解决问题

    【讨论】:

    • 当我将它保存为 .ps1 并运行时,它会创建 total.txt 但它是空白的
    • 是 打开 Powershell ISE 并将代码粘贴到那里进行测试。然后保存为ps1文件
    • 对不起,我编辑了评论。我保存为 ps1 并运行它。好像是循环遍历目录,但是 total.txt 是空的
    • 它说它是15kb,但是当我打开文件时,我什么也没看到?奇怪
    • 获取到第9列数据了吗?
    【解决方案3】:

    如果这是逗号分隔的输入,那就太简单了。所以我创建了一个简短的脚本来将数据转换为 CSV,以这种方式保存,然后我展示了一个简短的示例如何使用它。

    函数获取列数据 { 参数([字符串]$文件夹)

    $fileInput = Get-Content -Path "$folder\output.txt"
    
    # initialize an array
    $fileOutput = @()
    
    # put title headers for later import as CSV
    # I chose arbitrary names, they can be almost anything, but avoid spaces and special characters
    $fileOutput += "Column1,Column2,Column3,Column4,Column6,Column7,Column8,Column9,ColumnA"
    
    foreach ($line in $fileinput) {
        # here's the meat, trim whitespace off the end and replace spaces with commas
        $line = $line.TrimEnd() -replace ' ',','
    
        # add the results to the array
        $fileoutput += $line
        }
    # write to a csv
    # there's proabably another way to go from an array of strings to an array of objects, 
    #    but saving as a CSV, then importing the CSV is dirt simple
    $fileOutput | out-file 'C:\temp1\converted.csv'
    $Objects = import-csv 'C:\temp1\converted.csv'
    
    # Select the next to last column
    $Objects | Select -ExpandProperty Column9 
    }
    
        $location = "c:\temp1" #set to whatever local or remote path
        $folders = Get-ChildItem $location -Directory
        foreach ($folder in $folders) {
            if (Test-Path $folder\output.txt) {
                $folderColumn = Get-ColumnData $folder
                }
                # at this point you have a column of data but it's stored as a collection of strings
                # need to do something here to add it to your data file and I'm stuck there
            }    
    

    您需要从这里找到一种方法,从 $Objects 中获取一列,并将其作为您最终答案的一列。

    【讨论】:

      猜你喜欢
      • 2014-12-16
      • 2013-04-26
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      相关资源
      最近更新 更多