Question

I need to process a file that contains some lines beginning with a dash (-); these are continuation lines that need to be appended to the previous line. so what I have is something like:

  Lorem ipsum dolor sit amet, consectetur 
  - adipiscing elit. Donec 
  - consectetur lotis. 
  Sed a est dui.
  Curabitur placerat a tortor
  - vel sodales.

and what I want is:

Lorem ipsum dolor sit amet, consectetur - adipiscing elit. Donec - consectetur lotis.
Sed a est dui. 
Curabitur placerat a tortor - vel sodales.

so I've written something like this:

$s = ""
cat $f |%{
    if ($_ -match "^-") { $s += $_ } else { $s; $s = $_; }
} |out-file x.txt

My question is: in cases when the file ends with dashed lines, the script never outputs the final line, because upon receipt of the final line from the pipe, it doesn't know it's the final line.

In Perl I used to be able to use a construct like END {} to do these things. How would that be handled in Powershell?

Update

Perl is relevant because in that language I could do something like (where txt is a file containing the relevant text):

perl -lne '
   BEGIN { $s = "" };
   if (/^-/) { $s .= $_ } else { print $s; $s = $_; }
   END { print $s; }
   ' txt 

where, as you can see, the END{} construct solves my problem

Was it helpful?

Solution

Short version, using your code:

$s = ""
cat $f | % {
    if ($_ -match "^-") { $s += $_ } else { $s; $s = $_; }
} -end { $s } | out-file x.txt

longer version:

function glue {
   [CmdletBinding()]
   param(
      [Parameter(ValueFromPipeline=$true)]
      $line
   )
   begin { $output = "" }
   process { if ($line -match "^-") { $output += $line } else { $output; $output = $line; }
   end { $output }
}
cat $f | glue | out-file x.txt

OTHER TIPS

It isn't clear whether the leading spaces are preswent in your data source.

If your file is smaller than a few megabytes, then it is most simple to read all of the data into memory and remove the newlines.

This program demonstrates

use strict;
use warnings;

my $data = do {
  local $/;
  <DATA>;
};

$data =~ s/\s*\n\s+-/ -/g;

print $data;

__DATA__
Lorem ipsum dolor sit amet, consectetur 
  - adipiscing elit. Donec 
  - consectetur lotis. 
  Sed a est dui.
  Curabitur placerat a tortor vel sodales.

output

Lorem ipsum dolor sit amet, consectetur - adipiscing elit. Donec - consectetur lotis. 
  Sed a est dui.
  Curabitur placerat a tortor vel sodales.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top