Click here to Skip to main content
15,558,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All!

I am trying to create a powershell script that appends data to the beginning and end of each line. At the end I would like to count every 5 lines and join, then add a line break and count the next 5 and join, etc...

What I have tried:

Currently I have this:

$file = Read-Host -Prompt "Enter Path" #Prompts user to specify file path

$content = Get-Content $file #Gets content of the file

$beginning_content = Read-Host -Prompt "Please enter what to add at beginning of each line" #Enter what to append at beginning of string

$beginning_content_replace = $content -replace '^', $beginning_content #Make the replacement specified

$end_content = Read-Host -Prompt "Please enter what to add to the end of each line" #Enter what to append at end of string

$end_content_replace = $beginning_content_replace -replace '$', $end_content #Make the replacement

$final = $end_content_replace -join ' '

echo $final

I have tried using "-ReadCount 5" at the beginning of the script, but that will apply the changes to the 5 values together instead of individually. For example, if I have a text file with 30 lines of the word "test" and wanted to add double quotes to the beginning and end it will look like this:

"test test test test test"
"test test test test test"
......

I want it to look like this:

"test" "test" "test" "test" "test"
"test" "test" "test" "test" "test"
......

Is there a way to count every 5 lines and then perform the join operation without using Readcount?
Posted
Updated 7-Mar-21 23:15pm
Comments
Richard MacCutchan 8-Mar-21 3:49am    
When I run that script I get the following:
Enter Path: test.txt
Please enter what to add at beginning of each line: "
Please enter what to add to the end of each line: "
"test" "test" "test" "test" "test" "test" "test" "test" "test" "test"

I guess you need to add a loop that repeats every 5 lines. Take a look at PowerShell Basics: ForEach Loop & -object cmdlet | Code Examples[^].

1 solution

Try this:
PowerShell
$file = Read-Host -Prompt "Enter Path" #Prompts user to specify file path
$beginning_content = Read-Host -Prompt "Please enter what to add at beginning of each line" #Enter what to append at beginning of string
$end_content = Read-Host -Prompt "Please enter what to add to the end of each line" #Enter what to append at end of string

$content = Get-Content $file #Gets content of the file

$count = 0
$next = ''
ForEach ($line in $content) {
  $beginning_content_replace = $line -replace '^', $beginning_content #Make the replacement specified
  $end_content_replace = $beginning_content_replace -replace '$', $end_content #Make the replacement
  $next = $next + ' ' + $end_content_replace
  $count += 1
  if ($count -eq 5) {
    echo $next
    $next = ''
    $count = 0
  }
}
echo $next # any residual lines
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900