Solved

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

Posted on 2004-09-17
3
910 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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…
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.…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

14 Experts available now in Live!

Get 1:1 Help Now