Solved

Split file using perl and rename output files based on data in file

Posted on 2004-09-17
3
918 Views
Last Modified: 2011-09-20
Hi there,

I have an input file that is a concatenation of multiple files (variable length).
I am trying to write a perl script that will split the concatenated file into the multiple files that it is made up of.

E.g. the input file is as follows

<start>
customer-id="100"
data 1
<end>
<start>
customer-id="200"
data 2
<end>

The text "<start>" and "<end>" always delimit the files within the input file.

So in this case there would be two new files.

I also need to name the output files with the customer-id that is containned in the data.
So the first output file would be called, for example, 100.out and it would contain
<start>
customer-id="100"
data 1
<end>

and similarly for the second and ay subsequent files

Is all of this possible?

Thanks in advance.
0
Comment
Question by:ghev123
[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
3 Comments
 
LVL 6

Assisted Solution

by:holli
holli earned 200 total points
ID: 12082500
open IN, "filename" or die "file not opened";

while ( <IN> )
{
      push @lines, $_;
      $id = $1 if /customer-id="([0-9]+)"/;

      if ( /<end>/ )
      {
            print "OUT $id\n";
            open OUT, ">$id" or die "cannot write";
            print OUT @lines;
            close OUT;
            @lines = ();
      }
}

close IN;
0
 
LVL 5

Accepted Solution

by:
ZiaTioN earned 200 total points
ID: 12099477
#!/usr/bin/perl -w

use strict;

die &usage unless @ARGV;

open(FILE, "<", $ARGV[0]) || die "Can't open $ARGV[0]: $!\n";
my @data = <FILE>;
close(FILE);

my ($fName, @tmp);
for (@data) {
   push(@tmp, $_);
   $fName = $1 if (/^customer-id="(\w+)"/);
   if (($fName) && (/<end>/)) {
      my $file = $fName.".out";
      open(NF, ">", "C:\\$file") || die "Can't open $fName.out: $!\n";
      print NF $_ for (@tmp);
      close(NF);
      undef @tmp;
   }
}
print "Done!\n";

sub usage {
   my @path = split(/\//, $0);
   @path    = reverse(@path);
   my $app  = $path[0];
   print "Error! Required user input missing!\n";
   print "Usage: $app <target file>\n";
}

Pretty much the same concept as holli's, just a little more of a complete script taking user input and concatenating the file extension to the filename. Not to mention a shebang line and the needed "use strict;".
0
 

Author Comment

by:ghev123
ID: 12121247
Guys, this is great, many thanks for the answers. Both are very helpful.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

738 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