Powershell逐字阅读文本文件

时间:2016-11-05 01:24:12

标签: arrays powershell line

所以我正在尝试计算我的文本文件的单词,但是当我获取内容时,数组会逐字逐句地读取它们,所以它不会让我逐字逐句地比较它们。我希望你们能帮助我!

清除-主机     #Functions

function Get-Articles (){

 foreach($Word in $poem){
    if($Articles -contains $Word){
       $Counter++
    }
}
    write-host "The number of Articles in your sentence: $counter"
}

#Variables

$Counter = 0

$poem = $line
$Articles = "a","an","the"

#Logic

$fileExists = Test-Path "text.txt"

if($fileExists) {
    $poem = Get-Content "text.txt"
    }
else
    {
    Write-Output "The file SamMcGee does not exist"  
    exit(0) 
    }

$poem.Split(" ")

Get-Articles

2 个答案:

答案 0 :(得分:3)

你的脚本做了什么,稍微编辑了一下:

$poem = $line                    # set poem to $null (because $line is undefined)
$Articles = "a","an","the"       # $Articles is an array of strings, ok

                                 # check file exists (I skipped, it's fine)

$poem = Get-Content "text.txt"   # Load content into $poem, 
                                 # also an array of strings, ok

$poem.Split(" ")                 # Apply .Split(" ") to the array.
                                 # Powershell does that once for each line.
                                 # You don't save it with $xyz = 
                                 # so it outputs the words onto the 
                                 # pipeline.
                                 # You see them, but they are thrown away.

Get-Articles                     # Call a function (with no parameters)


function Get-Articles (){        

                                 # Poem wasn't passed in as a parameter, so
 foreach($Word in $poem){        # Pull poem out of the parent scope. 
                                 # Still the original array of lines. unchanged.
                                 # $word will then be _a whole line_.

    if($Articles -contains $Word){    # $articles will never contain a whole line
       $Counter++
    }
}
    write-host "The number of Articles in your sentence: $counter"  # 0 everytime
}

您可能希望$poem = $poem.Split(" ")使其成为单词而非行数。

或者你可以通过

将$ poem单词传递给函数
function Get-Articles ($poem) {
...

Get-Articles $poem.Split(" ")

您可以使用PowerShell管道:

$Articles = "a","an","the"

$poemArticles = (Get-Content "text.txt").Split(" ") | Where {$_ -in $Articles}
$counter = $poemArticles | Measure | Select -Expand Count
write-host "The number of Articles in your sentence: $counter"

答案 1 :(得分:1)

TessellatingHeckler's helpful answer很好地解释了您的方法存在的问题。

以下是您的命令的简化版本:

   <S:Body>
      <ns2:notifySmsDeliveryReceiptResponse xmlns:ns2="http://www.csapi.org/schema/parlayx/sms/notification/v2_2/local"/>
   </S:Body>

$counter = (-split (Get-Content -Raw text.txt) -match '^(a|an|the)$').count write-host "The number of articles in your sentence: $counter" 运算符的一元形式在这里是关键:它通过在单词之间的任何空白运行将输入拆分为单词,从而产生一个数组个别词语。

-split然后将生成的单词数组与匹配单词-matchaanthe的正则表达式匹配。

结果是输入数组的过滤子数组只包含感兴趣的单词,而^(a|an|the)$只返回该子数组的计数。