使用PowerShell收集某些数据列

时间:2015-05-18 16:34:14

标签: powershell

我遇到一些问题让Windows PowerShell做我想做的事情。基本上,我在当前文件夹中有24个子文件夹,名为Angle1,Angle2等,最高为角度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命令等的集合,但没有任何接近我想要的东西。

3 个答案:

答案 0 :(得分:2)

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

<asp:SqlDataSource ID="MonthUsed0" runat="server" , SelectCommand="SELECT DISTINCT dbo.[ID Inventory$].[Item Description], dbo.[ID Transactions This Week$].Location, dbo.[ID Transactions This Week$].[Trns Date], dbo.[ID Transactions This Week$].Budget, dbo.[ID Inventory$].[Item Number] FROM dbo.[ID Transactions This Week$] INNER JOIN dbo.[ID Inventory$] ON dbo.[ID Transactions This Week$].[Item Number] = dbo.[ID Inventory$].[Item Number] WHERE (dbo.[ID Transactions This Week$].Location = @Location) AND (dbo.[ID Transactions This Week$].Budget = @Trans_Budget_Code)"
UpdateCommand="UPDATE zBudgetDetails SET Budget_ID = @Budget_ID, Budget_Account_ID = @Budget_Account_ID, Amount = @Amount, Memo = @Memo WHERE (Budget_Detail_ID = @Budget_Detail_ID)">
  <SelectParameters>
    <asp:ControlParameter ControlID="GVMonthReport" Name="Location" PropertyName="SelectedValue" />
    <asp:ControlParameter ControlID="GVMonthReport" Name="Trans_Budget_Code" PropertyName="SelectedValue" />
  </SelectParameters>
  <UpdateParameters>
    <asp:Parameter Name="Budget_ID" Type="Int32" />
    <asp:Parameter Name="Budget_Account_ID" Type="Int32" />
    <asp:Parameter Name="Amount" Type="Decimal" />
    <asp:Parameter Name="Memo" Type="String" />
    <asp:Parameter Name="Budget_Detail_ID" Type="Int32" />
  </UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="MonthUsed0" style="margin-top: 1px">
  <Columns>
    <asp:BoundField DataField="Item Description" HeaderText="Item Description" SortExpression="Item Description" />
    <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
    <asp:BoundField DataField="Trns Date" HeaderText="Trns Date" SortExpression="Trns Date" />
    <asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget" />
    <asp:BoundField DataField="Item Number" HeaderText="Item Number" SortExpression="Item Number" />
  </Columns>
</asp:GridView>
<br />
<br />
<br />
<asp:GridView ID="GVMonthReport" runat="server" AllowPaging="True" AllowSorting="True" DataKeyNames="Trans_Budget_Code,Location" AutoGenerateColumns="False" CellPadding="4" DataSourceID="MonthReport" ForeColor="#333333" GridLines="None" style="margin-right: 0px; margin-top: 0px;">
  <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  <Columns>
    <asp:BoundField DataField="No#" HeaderText="No#" ReadOnly="True" SortExpression="No#" />
    <asp:BoundField DataField="Transaction Type" HeaderText="Transaction Type" ReadOnly="True" SortExpression="Transaction Type" />
    <asp:BoundField DataField="Trans_Budget_Code" HeaderText="Trans_Budget_Code" ReadOnly="True" SortExpression="Trans_Budget_Code" />
    <asp:BoundField DataField="Location" HeaderText="Location" ReadOnly="True" SortExpression="Location" />
    <asp:BoundField DataField="Supplier/Employee Name" HeaderText="Supplier/Employee Name" ReadOnly="True" SortExpression="Supplier/Employee Name" />
    <asp:BoundField DataField="Project Code" HeaderText="Project Code" ReadOnly="True" SortExpression="Project Code" />
    <asp:BoundField DataField="WOID" HeaderText="WOID" ReadOnly="True" SortExpression="WOID" />
    <asp:BoundField DataField="Transaction Date" HeaderText="Transaction Date" ReadOnly="True" SortExpression="Transaction Date" />
    <asp:BoundField DataField="Craft" HeaderText="Craft" ReadOnly="True" SortExpression="Craft" />
    <asp:BoundField DataField="Transaction Description" HeaderText="Transaction Description" ReadOnly="True" SortExpression="Transaction Description" />
    <asp:BoundField DataField="Purpose Description" HeaderText="Purpose Description" ReadOnly="True" SortExpression="Purpose Description" />
    <asp:BoundField DataField="Quantity" HeaderText="Quantity" ReadOnly="True" SortExpression="Quantity" />
    <asp:BoundField DataField="Unit Cost" HeaderText="Unit Cost" ReadOnly="True" SortExpression="Unit Cost" />
    <asp:BoundField DataField="Total Cost" HeaderText="Total Cost" ReadOnly="True" SortExpression="Total Cost" />
    <asp:BoundField DataField="Purchased by" HeaderText="Purchased by" ReadOnly="True" SortExpression="Purchased by" />
    <asp:CommandField ShowSelectButton="True" />
  </Columns>
  <EditRowStyle BackColor="#999999" />
  <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  <SortedAscendingCellStyle BackColor="#E9E7E2" />
  <SortedAscendingHeaderStyle BackColor="#506C8C" />
  <SortedDescendingCellStyle BackColor="#FFFDF8" />
  <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<br />
<asp:SqlDataSource ID="MonthReport" runat="server" SelectCommand="SELECT No#, [Transaction Type], Trans_Budget_Code, Location, [Supplier/Employee Name], [Project Code], WOID, [Transaction Date], Craft, [Transaction Description], [Purpose Description], Quantity, [Unit Cost], [Total Cost], [Purchased by] FROM dbo.[MD Transactions] WHERE ([WO Budget Code] = @Acount) AND (YEAR([Transaction Date]) = @yeardate) AND (MONTH([Transaction Date]) = @monthdate) AND (Trans_Budget_Code = 'Maintenance') UNION ALL SELECT No#, [Transaction Type], Trans_Budget_Code, Location, [Supplier/Employee Name], [Project Code], WOID, [Transaction Date], Craft, [Transaction Description], [Purpose Description], Quantity, [Unit Cost], [Total Cost], [Purchased by] FROM dbo.[MD Transactions This Week$] WHERE ([WO Budget Code] = @Acount) AND (YEAR([Transaction Date]) = @yeardate) AND (MONTH([Transaction Date]) = @monthdate) AND (Trans_Budget_Code = 'Maintenance')"
UpdateCommand="UPDATE zBudgetDetails SET Budget_ID = @Budget_ID, Budget_Account_ID = @Budget_Account_ID, Amount = @Amount, Memo = @Memo WHERE (Budget_Detail_ID = @Budget_Detail_ID)">
  <SelectParameters>
    <asp:ControlParameter ControlID="DDLMonthAcount" Name="Acount" PropertyName="SelectedValue" />
    <asp:ControlParameter ControlID="DDLYear" Name="yeardate" PropertyName="SelectedValue" />
    <asp:ControlParameter ControlID="DDLMonth" Name="monthdate" PropertyName="SelectedValue" />
  </SelectParameters>
  <UpdateParameters>
    <asp:Parameter Name="Budget_ID" Type="Int32" />
    <asp:Parameter Name="Budget_Account_ID" Type="Int32" />
    <asp:Parameter Name="Amount" Type="Decimal" />
    <asp:Parameter Name="Memo" Type="String" />
    <asp:Parameter Name="Budget_Detail_ID" Type="Int32" />
  </UpdateParameters>
</asp:SqlDataSource>

答案 1 :(得分:0)

试试这个

            $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

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

答案 2 :(得分:0)

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

function Get-ColumnData {     param([string] $ folder)

$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中获取一列,并将其作为最终答案的列。