Solved

combine multiple lines

Posted on 2016-11-04
2
73 Views
Last Modified: 2016-11-04
Hi I have a file and I need to combine the lines

example:

From this

START=so_documentation Upd so_doc_source=M so_module=AGENT_LOG_ROLLOVER so_doc_\
START=type=Module


to this

START=so_documentation Upd so_doc_source=M so_module=AGENT_LOG_ROLLOVER so_doc_type=Module

Not sure the code would be since on multiple lines.

There could be multiple lines as the text wraps at 8 characters.
Its basically un-wraping the lines.

Thanks

Mike
0
Comment
Question by:mikeysmailbox1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
JesterToo earned 500 total points
ID: 41874870
This may not be the most elegant code (it is the first perl script I've ever written), but it appears to work with data based on the pattern of the limited sample you provided...

use strict;
use warnings;

my $filein    = 'datain.txt';
my $fileout   = 'dataout.txt';

my $newline   = '';
my $firstpart = '';
my $lastchar  = '';
my $firstequal = 0;
my $state      = 0;
my $backslash  = "\\";
my $trash      = '';

open(my $fhin, '<:encoding(UTF-8)', $filein)
  or die "Could not open file '$filein' $!";

open(my $fhout, '>:encoding(UTF-8)', $fileout)
  or die "Could not open file '$fileout' $!";

while (my $row = <$fhin>) {
  chomp $row;
  $firstequal = index $row, '=';
  $firstequal++;
  $lastchar = substr $row, -1;

  $state = 0;
  if (($newline eq '') && ($lastchar ne $backslash)) {
    $state = 1;
     }
  if (($newline eq '') && ($lastchar eq $backslash)) {
     $state = 2;
     }
  if (($newline ne '') && ($lastchar ne $backslash)) {
     $state = 3;
     }
  if (($newline ne '') && ($lastchar eq $backslash)) {
     $state = 4;
     }

  if ($state == 1) {
     print $fhout $row . "\n";
     }

  if ($state == 2) {
     $trash = chop($row);
     $newline = $newline . $row;;
     }

  if ($state == 3) {
     $firstpart = (substr $row, $firstequal);
     print $fhout $newline . $firstpart . "\n";
     $newline = '';
     }

  if ($state == 4) {
     $firstpart = (substr $row, $firstequal);
     $trash = chop($firstpart);
     $newline = $newline . $firstpart;
     }
}

close $fhout;
close $fhin;

Open in new window

1
 
LVL 1

Author Closing Comment

by:mikeysmailbox1
ID: 41875035
The worked great. Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question