Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-17
3
Medium Priority
?
920 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 800 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 800 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

704 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