PowerShell: How to add a new blank line at every 30 words in several text files that contains 1000 words?

Suzana Eree 811 Reputation points
2021-04-07T09:59:07.497+00:00

hello, is is possible to add a new blank line at every 30 words in several text files that contains 1000 words using PowerShell?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,323 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 28,976 Reputation points Microsoft Vendor
    2021-04-08T04:29:32.677+00:00

    Hi,

    Please check to see if this works.

    $files = "C:\temp\file1.txt","C:\temp\file2.txt"  
    $results = "C:\temp\result1.txt","C:\temp\result2.txt"  
    for($i=0;$i -lt $files.Count;$i++){  
        $content = Get-Content -Path $files[$i] -Raw  
        [string]$output = $null  
        $count = 0  
        for($j=0;$j -lt $content.Length;$j++){         
            $output += $content[$j]  
            if(($content[$j] -eq " ") -or ($content[$j] -eq "`r") -or ($content[$j] -eq "`n")){  
                if(-not $whitespace){           
                    $count ++  
                }  
                if($count%30 -eq 0){  
                    $output += "`r`n"  
                }  
                $whitespace = $true  
            }  
            else{  
                $whitespace = $false  
            }  
        }  
        $output | Out-File -FilePath $results[$i]  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3 additional answers

Sort by: Most helpful
  1. Chris 651 Reputation points
    2021-04-08T12:08:06.67+00:00

    Suzana,
    you can also test and try it with split

    $output = $null
    $count = $null
    $file = Get-Content C:\temp\file2.txt
    $array = $file -split(" ")
    
    foreach($word in $array){
        $output += $word
        $count ++
        if($count%30 -eq 0){
            $output += "`r`n"
        }
        else{
            if ($word -ne $array[-1])  # check if last word no space needed
            {
                $output +=" "
            }
        }
    }
    $output | Out-File C:\temp\result1.txt
    

  2. Chris 651 Reputation points
    2021-04-08T17:11:14.79+00:00

    yes, i know. Its only an example ;-)

    you need a second foreach loop and get-childitem to read all files

    $allfiles = Get-ChildItem c:\temp\*.txt 
    foreach($onefile in $allfiles){ 
       #your other code 
       $result = "result_" + $onefile.name 
       $outfile | out-file c:\temp\$result 
    } 
    

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 28,976 Reputation points Microsoft Vendor
    2021-04-09T01:54:36.767+00:00

    Hi @Suzana Eree ,

    You only need to get the file paths at the beginning.

    $filesdir = "C:\temp\files"  
    $resultsdir = "C:\temp\results"  
    $files = Get-ChildItem -Path $filesdir -File *.txt  
    foreach($file in $files){  
        $content = Get-Content -Path $file.FullName -Raw  
        [string]$output = $null  
        $count = 0  
        for($j=0;$j -lt $content.Length;$j++){         
            $output += $content[$j]  
            if(($content[$j] -eq " ") -or ($content[$j] -eq "`r") -or ($content[$j] -eq "`n")){  
                if(-not $whitespace){           
                    $count ++  
                }  
                if($count%30 -eq 0){  
                    $output += "`r`n`r`n"  
                }  
                $whitespace = $true  
            }  
            else{  
                $whitespace = $false  
            }  
        }  
        $output | Out-File -FilePath $resultsdir\$($file.name)  
    }