Solved

combine multiple lines

Posted on 2016-11-04
2
24 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
2 Comments
 
LVL 21

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
 

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now