PowerShell:在几个文本文件上每 50 个单词后添加一个新的空行
PowerShell: Add a new blank line after every 50 words on several text files
我想在多个文本文件的每 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
}
$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"
}
我想在多个文本文件的每 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
}
$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"
}