PowerShell:在几个文本文件上每50个单词后添加一个新的空行

PowerShell:在几个文本文件上每50个单词后添加一个新的空行,powershell,shell,unix,git-bash,Powershell,Shell,Unix,Git Bash,我想在几个文本文件中每50个单词后添加一个新的空行。我编写了这段代码,几乎可以正常工作,它使用相同的名称创建了新文件,但没有使用主文件的更改进行解析。谁能帮我一点忙吗 $allfiles = Get-ChildItem c:\Folder1\*.txt foreach($onefile in $allfiles){ $array = $file -split(" ") foreach($word in $array){ $outpu

我想在几个文本文件中每50个单词后添加一个新的空行。我编写了这段代码,几乎可以正常工作,它使用相同的名称创建了新文件,但没有使用主文件的更改进行解析。谁能帮我一点忙吗

 $allfiles = Get-ChildItem c:\Folder1\*.txt
  foreach($onefile in $allfiles){
  $array = $file -split(" ")
        
  foreach($word in $array){
      $output += $word
      $count ++
      if($count%50 -eq 0){
          $output += "`r`n`r`n"
      }
      else{
          if ($word -ne $array[-1])  # check if last word no space needed
          {
              $output +=" "
          }
      }
  }
  $result = "result_" + $onefile.name 
      $output | out-file c:\Folder1\$result 
  }

如果只是为了
powershell
,为什么会有一个
bash
标记?只要是powershellnp,很乐意帮助。它起作用了吗@希腊克雷尼库
$allfiles = Get-ChildItem c:\Folder1\*.txt

foreach($onefile in $allfiles)
{
    $result=[system.collections.generic.list[string]]::new()
    
    $words=(cat $onefile) -split '\s'
    $z=49

    for($i=0;$i -lt $words.count;$i+=50)
    {
        $result.Add(($words[$i..$z] -join ' ').trim())
        $result.Add("`r`n")
        $z=$z+50
    }

    $result | out-file "c:\Folder1\result_$($onefile.name).txt"
}