简体   繁体   English

正确的语法? 缺少括号但在命令中只有6个括号的Powershell错误?

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

the Powershell line: Powershell系列:

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

The ERROR message: 错误消息:

At line:1 char:114 在线:1个字符:114

  • ... " ",2)[0]} | ...“”,2)[0]} | ForEach-Object {$rgbArray = @($_)} | ForEach对象{$ rgbArray = @($ _)} | for( $i = 0; $i -le... for($ i = 0; $ i -le ...
  • ~ Missing closing ')' in expression. 〜表达式中缺少结尾的')'。 At line:1 char:150 在线:1个字符:150
  • ... y = @($_)} | ... y = @($ _)} | for( $i = 0; $i -le $rgbArray.length - 1; $i++ ) {$rgbAr... for($ i = 0; $ i -le $ rgbArray.length-1; $ i ++){$ rgbAr ...

  • ~ Unexpected token ')' in expression or statement. 〜表达式或语句中出现意外的标记')'。

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

The hist3.txt: hist3.txt:

2 1388581  
3 1449812  
4 63829  
5 10477  
6 5878
7 6703  
8 105349
9 8639  
10 7270
  1. So I am loading this text file : (Get-Content hist3.txt) 因此,我正在加载此文本文件: (Get-Content hist3.txt)
  2. I need to isolate the first word of each line: ForEach-Object { $_.split(" ",2)[0]} 我需要隔离每行的第一个单词: ForEach-Object { $_.split(" ",2)[0]}
  3. save each first word into a single array: ForEach-Object { $rgbArray = @($_)} 将每个第一个单词保存到单个数组中: ForEach-Object { $rgbArray = @($_)}
  4. then iterate over that array and access each element one by one : for( $i = 0; $i -le $rgbArray.length - 1; $i++ ) {$rgbArray[$i]} 然后遍历该数组并逐个访问每个元素: for( $i = 0; $i -le $rgbArray.length - 1; $i++ ) {$rgbArray[$i]}

my above code seems to be missing a parenthesis somehow. 我上面的代码似乎以某种方式缺少括号。 or maybe it doesn't recognize the $rgbArray later in the pipe? 还是稍后在管道中无法识别$ rgbArray?

can you tell me what information i am missing or need to read up on? 您能告诉我我缺少或需要阅读哪些信息吗? How do i get this working? 我该如何工作?

the end goal is to actually load the elements into the array and then compare several of the elements to other elements in the same array. 最终目标是将元素实际加载到数组中,然后将几个元素与同一数组中的其他元素进行比较。 so i would be accessing multiple parts of the same array at once. 所以我将一次访问同一数组的多个部分。

Very new to powershell so thank you for your help. 对Powershell来说还很陌生,因此感谢您的帮助。

You can't pipe anything into for . 您无法将任何内容传送到for About For describes that it's a language command not a cmdlet. 关于For描述它是语言命令而不是cmdlet。

Hard to guess your final aim. 很难猜出你的最终目标。 For start, try next code snippets in sequence to see what happens: 首先,请按顺序尝试下一个代码片段以查看发生了什么:

Get-Content 'hist3.txt'

then 然后

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

and then 接着

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

The latter is the same as next one-liner: 后者与下一个直线相同:

(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