Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: split one file into many on finding a new line

by kubrat (Scribe)
on Sep 15, 2008 at 09:57 UTC ( #711419=note: print w/ replies, xml ) Need Help??


in reply to Re^2: split one file into many on finding a new line
in thread split one file into many on finding a new line

change this  while ( $temp_line = <INFILE> ) to this  while ( <INFILE> ) and it will work.

Also, note that if you want the filenames to be generated as MyFileName00* you'll have to drop the path from your $filename variable.


Comment on Re^3: split one file into many on finding a new line
Select or Download Code
Re^4: split one file into many on finding a new line
by smarter_aries (Novice) on Sep 15, 2008 at 14:12 UTC
    Thanks Kubrat, thanks all, I am able to separate the files now, code for the same as is as follows:
    #!/sw/perl5/1.1/bin/perl use strict; my $filename = "MyFileName0000"; local $/ = "\n\n"; my $temp_line; my $filehandle; open ( INFILE, $filename ) || ( die "Cant read $filename: $!"); while ( <INFILE> ) { chomp; ++$filename; print "**** Open file $filename here\n"; open ( IFILE, ">>$filename" ); print IFILE "$_\n"; close(IFILE); print "**** Close file $filename here\n"; } select INFILE; close(INFILE);
    but the files created are MyFileName0001,MyFileName0002 etc...is there a way i can have the file anme as the tabels name itself...i tried following:
    #!/sw/perl5/1.1/bin/perl use strict; my $filename = "MyFileName0000"; local $/ = "\n\n"; my $scriptname; my $filehandle; open ( INFILE, $filename ) || ( die "Cant read $filename: $!"); while (<INFILE> ) { chomp; #++$filename; $scriptname=~ /CREATE TABLE ([\S]*)/; print "**** Open file $scriptname here\n"; open ( IFILE, ">>$scriptname" ); print IFILE "$_\n"; close(IFILE); print "**** Close file $scriptname here\n"; } select INFILE; close(INFILE);
    But the scriptname is not being extracted properly..i am getting null.... Thanks
      You can also write it as a one-line command:
      perl -00ne 'BEGIN { $filename = "MyFileName0000" } { open my $IFILE, " +>", $filename++; print $IFILE $_ }' table1.txt table2.txt tableN.txt

      • -00 means separate on empty lines (paragraph mode) -- see cdarke's reply
      • -n means implicit while (<ARGV>) { .. }
      • -e uses an expression as the program instead of a full-fledge script
      • The BEGIN { .. } block sets $filename at the beginning, outside of the implicit while loop
      • Creating an explicit scope { open my $IFILE, .. } means we do not have to explicitly close($IFILE) since $IFILE is only effective within the curly braces -- once out-of-scope, $IFILE is auto-closed

      To get a feel of the code, you can run this on its own:

      perl -MO=Deparse -00ne 'BEGIN { $filename = "MyFileName0000" } { open +my $IFILE, ">", $filename++; print $IFILE $_ }'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://711419]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-11-23 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls