Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with simple Perl program 1

Status
Not open for further replies.

jsilva1

Technical User
Nov 21, 2009
2
US
Hello.
I am a new Perl user (not a student but research staff) and I'm trying to write a Perl program to manipulate some data files. I've used the split commands before to manipulate columns and rows but this is not working for my current need.

I have a program that outputs data in one continous column. For example if I have the program run two simulations (each containing 1000 data values), the program output is one continous column of 2000 data values.

What I am trying to do is have the Perl program generate 2 text files for each simulation each containing their respective column of 1000 data values.

The actual simulations I'm running contain over 500,000 data values each, so I need a perl script to handle it.

Many thanks for any help you can provide. The beginnings of the program I'm writing is below.


#!/usr/bin/perl -w

#Program reads GSLIB simulation output (continuous column of numbers) and writes individual
#output files for a user-defined number of GSLIB realizations
#
#GSLIB program outputs multiple realizations in a single continous column of data values.
#For example, if one realization contains 1000 data values and GSLIB is set to run 2 simulations,
#the output (*.txt file) will contain a continuous column of 2000 data values.
#What I'm trying to do is allow myself to enter the number of realizations (realiz) and the number
#of data values for each realization (size) and have the program output separate text files for
#each realization

@ARGV ++ 3 or die "Usage: extract.pl realiz size [in.file]\n";

$realiz = $ARGV[0];
$size = $ARGV[1];
$InFile = $ARGV[2];


# Open the input file (or die trying)

open(INPUT,"<$InFile") or die "Can't open file \"$file\".\n";

#loop through the file; start printing when
while (<INPUT>) {
chomp;

$count=0;
while ()
{
$count++;
if ($count <= $size
 
Just a suggestions, not tested, you'll probably need to tweak for the output you want.

Code:
open(INPUT, "<$InFile");
 @input = <INPUT>;
 chomp @input;
 close INPUT;
my $start = 0;
for my $num (@realiz) {
 open(FILE ">FILE.$num);
 for my $num2($start..$size) {
   print FILE "$input[$num2]\n";
 }
 close FILE;
 $start += $size;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Personally I would choose awk for this task:

Code:
awk 'BEGIN{f=1}{print >> "of"f;close "of"f}!(NR%1000){f++}' inputfile

That would create files called of1, of2, etc.

Annihilannic.
 
Thanks. I downloaded Cygwin and ran the awk script. I'm getting a syntax error on the first quotation after the close statement
 
Hi

Or something similar in [tt]perl[/tt] :
Code:
perl -ne 'if($.%1000==1){close F if F;open F,">>of".++$f}print F' inputfile
Note that opening and closing the output file 1000 times is not the fastest solution... The [tt]awk[/tt] solution I would write like this :
Code:
awk 'NR%1000==1{close("of"++f)}{print>>"of"f}' inputfile

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top