簡體   English   中英

正確的語法? 缺少括號但在命令中只有6個括號的Powershell錯誤?

[英]Proper Syntax? Powershell error for missing parenthesis but only 6 parenthesis in command?

Powershell系列:

 (Get-Content hist3.txt)  |  ForEach-Object { $_.split(" ",2)[0]}  |  ForEach-Object { $rgbArray = @($_)} | for( $i = 0; $i -le $rgbArray.length - 1; $i++ ) {$rgbArray[$i]}

錯誤消息:

在線:1個字符:114

  • ...“”,2)[0]} | ForEach對象{$ rgbArray = @($ _)} | for($ i = 0; $ i -le ...
  • 〜表達式中缺少結尾的')'。 在線:1個字符:150
  • ... y = @($ _)} | for($ i = 0; $ i -le $ rgbArray.length-1; $ i ++){$ rgbAr ...

  • 〜表達式或語句中出現意外的標記')'。

    • CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException
    • FullyQualifiedErrorId:MissingEndParenthesisInExpression

hist3.txt:

2 1388581  
3 1449812  
4 63829  
5 10477  
6 5878
7 6703  
8 105349
9 8639  
10 7270
  1. 因此,我正在加載此文本文件: (Get-Content hist3.txt)
  2. 我需要隔離每行的第一個單詞: ForEach-Object { $_.split(" ",2)[0]}
  3. 將每個第一個單詞保存到單個數組中: ForEach-Object { $rgbArray = @($_)}
  4. 然后遍歷該數組並逐個訪問每個元素: for( $i = 0; $i -le $rgbArray.length - 1; $i++ ) {$rgbArray[$i]}

我上面的代碼似乎以某種方式缺少括號。 還是稍后在管道中無法識別$ rgbArray?

您能告訴我我缺少或需要閱讀哪些信息嗎? 我該如何工作?

最終目標是將元素實際加載到數組中,然后將幾個元素與同一數組中的其他元素進行比較。 所以我將一次訪問同一數組的多個部分。

對Powershell來說還很陌生,因此感謝您的幫助。

您無法將任何內容傳送到for 關於For描述它是語言命令而不是cmdlet。

很難猜出你的最終目標。 首先,請按順序嘗試下一個代碼片段以查看發生了什么:

Get-Content 'hist3.txt'

然后

Get-Content 'hist3.txt' | ForEach-Object { $_.split(" ",2)[0]}

接着

Get-Content 'hist3.txt' | ForEach-Object { $_.split(" ",2)[0]} | 
   ForEach-Object { 
        $rgbArray = @($_) 
        for( $i = 0; $i -le $rgbArray.length - 1; $i++ ) {$rgbArray[$i]}
   }

后者與下一個直線相同:

(Get-Content 'D:\PShell\Downloaded\hist3.txt.txt' )  |  ForEach-Object { $_.split(" ",2)[0]} |  ForEach-Object { $rgbArray = @($_); for( $i = 0; $i -le $rgbArray.length - 1; $i++ ) {$rgbArray[$i]}}
#you can do simply that:
Import-Csv 'hist3.txt' -Delimiter ' ' -Header Col1, Col2 | select Col1


#or this
Get-Content 'hist3.txt' | %{ ($_ -split ' ')[0]}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM