Home > Net >  Join every other line in Powershell
Join every other line in Powershell

Time:02-05

I want to combine every other line from the input below. Here is the input.

ALPHA-FETOPROTEIN      ROUTINE    CH 0203 001   02/03/2023@10:45 LIVERF3
     ###-##-####    #######,####        In lab
ALPHA-FETOPROTEIN      ROUTINE    CH 0203 234   02/03/2023@11:05 LIVER
     ###-##-####    ########,########   In lab
ANION GAP              STAT       CH 0203 124   02/03/2023@11:06 DAY
     ###-##-####    ######,##### ####   In lab
BASIC METABOLIC PANE   ROUTINE    CH 0203 001   02/03/2023@10:45 LIVERF3
     ###-##-####    #######,#### ###### In lab

This is the desired output

ALPHA-FETOPROTEIN      ROUTINE    CH 0203 001   02/03/2023@10:45 LIVERF3 ###-##-####    #######,####        In lab
ALPHA-FETOPROTEIN      ROUTINE    CH 0203 234   02/03/2023@11:05 LIVER ###-##-####    ########,########   In lab
ANION GAP              STAT       CH 0203 124   02/03/2023@11:06 DAY ###-##-####    ######,##### ####   In lab
BASIC METABOLIC PANE   ROUTINE    CH 0203 001   02/03/2023@10:45 LIVERF3 ###-##-####    #######,#### ###### In lab

The code that I have tried is

for($i = 0; $i -lt $splitLines.Count; $i  = 2){
  $splitLines[$i,($i 1)] -join ' '
}

It came from Joining every two lines in Powershell output. But I can't seem to get it to work for me. I'm not well versed with powershell, but i'm at the mercy of what's available at work.

Edit: Here is the entire code that I am using as requested.

# SET VARIABLES
$inputfile = "C:\Users\Will\Desktop\testfile.txt"
$outputfile = "C:\Users\Will\Desktop\testfileformatted.txt"
$new_output = "C:\Users\Will\Desktop\new_formatted.txt"

# REMOVE EXTRA CHARACTERS
$remove_beginning_capture = "-------------------------------------------------------------------------------"
$remove_end_capture = "==============================================================================="
$remove_line = "------"
$remove_strings_with_spaces = "            \d"
Get-Content $inputfile | Where-Object {$_ -notmatch $remove_beginning_capture} | Where-Object {$_ -notmatch $remove_end_capture} | Where-Object {$_ -notmatch $remove_line} | Where-Object {$_ -notmatch $remove_strings_with_spaces}  | ? {$_.trim() -ne "" } | Set-Content $outputfile

# Measures line length for loop
$file_lines = gc $outputfile | Measure-Object

#Remove Whitespace
# $whitespace_removed = (Get-Content $outputfile -Raw) -replace '\s ', ' '| Set-Content -Path C:\Users\Will\Desktop\new_formatted.csv

# Combine every other line
$lines = Get-Content $outputfile -Raw
$newcontent = $lines.Replace("`n","")
Write-Host "Content: $newcontent"
$newcontent | Set-Content $new_output

for($i = 0; $i -lt $splitLines.Count; $i  = 2){
  $splitLines[$i,($i 1)] -join ' '
}

CodePudding user response:

Just read two lines and then print one

$inputFilename = "c:\temp\test.txt"
$outputFilename = "c:\temp\test1.txt"

$reader = [System.IO.StreamReader]::new($inputFilename)
$writer = [System.IO.StreamWriter]::new($outputFilename)
while(($line = $reader.ReadLine()) -ne $null)
{
   $secondLine = ""
   if(!$reader.EndOfStream){ $secondLine = $reader.ReadLine() }

   $writer.WriteLine($line   $secondLine)
}
$reader.Close()
$writer.Flush()
$writer.Close()
  • Related