Patrik
Patrik

Reputation: 855

Combine continuation lines starting with "+"

I know this is a question that was asked many times, I didn't find an answer that answers my specific question.

Essentially, I have an input file where some lines begin with a + string. These lines must be appended at the end of the previous line and the + character must be removed. So

abc
def
+ ghj
klm

should become

abc
def ghj
klm

This is the perl command I crafted by looking at other answers

perl -pe 's/\n\+ //m' < input

but it prints the input file without changing it. The odd thing is that, on websites like regex101.com, my regex produces the expected result.

What am I missing?

Upvotes: 2

Views: 231

Answers (3)

Borodin
Borodin

Reputation: 126742

Presumably these are relatively small files and so will easily fit into memory?

This is the simplest way

use strict;
use warnings 'all';

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

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

print $data;

__DATA__
abc
def
+ ghj
klm

output

abc
def ghj
klm

In a Perl one-liner that would be

perl -0777 -pe 's/\n\+//g' < input

Upvotes: 3

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89574

You can change the input record separator $/ to the string "\n+" (or "\r\n+" for a Windows file), then you only need to chomp each records:

perl -pe 'BEGIN{$/="\n+"}chomp' file

Upvotes: 2

Jay.Zhao
Jay.Zhao

Reputation: 491

@Borodin has provided a good solution for your question. Besides, if you need to update that into the old file , just add "-i":

perl -0777 -i -pe 's/\n\+//g' input

or if you want to keep an source file as backup ,use:

perl -0777 -i.bak -pe 's/\n\+//g' input

this will generate a file named input.bak in case you want to recovery.

Upvotes: 1

Related Questions