Solved

combine multiple lines

Posted on 2016-11-04
2
61 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
 
LVL 1

Author Closing Comment

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 128
compress files in RAR using perl 13 81
read an xml file in perl 2 32
Able to retrieve only 1 row through email amongst multiple rows 3 36
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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