Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

790 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