Need File I/O Help....

R

RodFGWA

I need help with file manipulation in Perl.

I want to open a text file that contains the names and locations of
about 650 text files that I need to evaluate.

C:\DIRA\SUBDIRA\FILENAMEZZ.txt
C:\DIRB\SUBDIRX\FILENAMEYY.txt

I want to open each file in the text file and print the first ten lines
in the file to an OUTPUT FILE ( BY APPENDING ).

I then want to print a distinct seperator, like "*****".

This doesn't have to run very fast.

I know the code below only prints the string from the file, but I was
just testing the beginning code.

I have the following code so far:

# Define the source file for evaluation
$input_file = 'SR20051004.txt';

# Open the file
open(INPUT_FILE, $input_file);

# NOTE:
#
# HERE I WANT TO OPEN A FILE FOR APPENDING
#
#

# Read it into an array
@lines = <INPUT_FILE>;

# Close the File
close(INPUT_FILE);

foreach $line (@lines)
{
# HERE IS WHERE I NEED HELP
#
# I WANT TO OPEN EACH FILE $line
#
# I WANT TO OUTPUT THE FIRST TEN LINES TO THE OUTPUT FILE
#
# I WANT TO PRINT MY SEPERATOR TO THE FILE
#
# I WANT TO CLOSE EACH FILE

print "$line";
}

Your help would benefit me greatly as I am completely an amatuer wit
Perl but know that ir is great with this sort of activity.

I am having trouble finding any information on opening files from the
data contained in variables.
 
J

John Bokma

RodFGWA said:
I need help with file manipulation in Perl.

I want to open a text file that contains the names and locations of
about 650 text files that I need to evaluate.

C:\DIRA\SUBDIRA\FILENAMEZZ.txt
C:\DIRB\SUBDIRX\FILENAMEYY.txt

I want to open each file in the text file and print the first ten
lines in the file to an OUTPUT FILE ( BY APPENDING ).

I then want to print a distinct seperator, like "*****".

This doesn't have to run very fast.

I know the code below only prints the string from the file, but I was
just testing the beginning code.

I have the following code so far:

You forgot:

use strict;
use warnings;

read the posting guidelines otherwise you might end up with a discussion
about you and your posting, not your problem.
# Define the source file for evaluation
$input_file = 'SR20051004.txt';

# Open the file
open(INPUT_FILE, $input_file);

perldoc -f open and read what it returns.
# HERE I WANT TO OPEN A FILE FOR APPENDING

I am sure that it's documented in the above page as well. Also, note
that if you run your script twice with the same input file (accidently),
it appends the same info.
# Read it into an array
@lines = <INPUT_FILE>;
# Close the File
close(INPUT_FILE);

perldoc -f close, read what it returns. Yes, I can't think up what kind
of error a close after a read might return, but better safe then sorry
:)
foreach $line (@lines)
{
# HERE IS WHERE I NEED HELP
#
# I WANT TO OPEN EACH FILE $line

You already know how to do this
# I WANT TO OUTPUT THE FIRST TEN LINES TO THE OUTPUT FILE

my $read = 0;
while ( my $line = <$input> ) {

print $output $line;
last if ++$read == 10;
}
# I WANT TO CLOSE EACH FILE

you already know how to do that (I would do that before: )
# I WANT TO PRINT MY SEPERATOR TO THE FILE

print $output "*****\n";
I am having trouble finding any information on opening files from the
data contained in variables.

Now don't start lying, you already did it:

open(INPUT_FILE, $input_file);
^^^^^^^^^^^^
 
J

John W. Krahn

RodFGWA said:
I need help with file manipulation in Perl.

I want to open a text file that contains the names and locations of
about 650 text files that I need to evaluate.

C:\DIRA\SUBDIRA\FILENAMEZZ.txt
C:\DIRB\SUBDIRX\FILENAMEYY.txt

I want to open each file in the text file and print the first ten lines
in the file to an OUTPUT FILE ( BY APPENDING ).

I then want to print a distinct seperator, like "*****".

This doesn't have to run very fast.

head -n 10 `cat text_file` >> OUTPUT_FILE



Oh, you want that in Perl.

#!/usr/bin/perl
use warnings;
use strict;

open FILE, '<', 'text_file' or die "Cannot open 'text_file' $!";

chomp( @ARGV = <FILE> );

close FILE;


open OUT, '>>', 'OUTPUT_FILE' or die "Cannot open 'OUTPUT_FILE' $!";

my $lines = 10;

while ( <> ) {

$. == 1 && print OUT "*****\n";

$. <= $lines && print OUT $_;

$. > $lines || eof and close ARGV;

}

__END__




John
 
T

Tintin

RodFGWA said:
I need help with file manipulation in Perl.

I want to open a text file that contains the names and locations of
about 650 text files that I need to evaluate.

C:\DIRA\SUBDIRA\FILENAMEZZ.txt
C:\DIRB\SUBDIRX\FILENAMEYY.txt

I want to open each file in the text file and print the first ten lines
in the file to an OUTPUT FILE ( BY APPENDING ).

I then want to print a distinct seperator, like "*****".

This doesn't have to run very fast.

I know the code below only prints the string from the file, but I was
just testing the beginning code.

I have the following code so far:

# Define the source file for evaluation
$input_file = 'SR20051004.txt';

# Open the file
open(INPUT_FILE, $input_file);

# NOTE:
#
# HERE I WANT TO OPEN A FILE FOR APPENDING
#
#

# Read it into an array
@lines = <INPUT_FILE>;

# Close the File
close(INPUT_FILE);

foreach $line (@lines)
{
# HERE IS WHERE I NEED HELP
#
# I WANT TO OPEN EACH FILE $line
#
# I WANT TO OUTPUT THE FIRST TEN LINES TO THE OUTPUT FILE
#
# I WANT TO PRINT MY SEPERATOR TO THE FILE
#
# I WANT TO CLOSE EACH FILE

print "$line";
}

Your help would benefit me greatly as I am completely an amatuer wit
Perl but know that ir is great with this sort of activity.

I am having trouble finding any information on opening files from the
data contained in variables.

#!/usr/bin/perl
use warnings;
use strict;

my $filelist = "/path/to/SR20051004.txt";
my $output = "/path/to/output.txt";

open INPUT,$filelist or die "Can not open $filelist $!\n";
open OUTPUT,">>$output" or die "Can not open $output for appending $!\n";

while (<INPUT>) {
chomp;
open FILE, $_ or die "Can not open $_ $!\n";

while (<FILE>) {
print OUTPUT;
last if $. == 10;
}

close FILE;
}
 
R

RodFGWA

Thank you all for your replies. I appreciate the time you have spent on
my problem.

Suppose I use the following solution:

#!/usr/bin/perl
use warnings;
use strict;


open FILE, '<', 'text_file' or die "Cannot open 'text_file' $!";


chomp( @ARGV = <FILE> );


close FILE;


open OUT, '>>', 'OUTPUT_FILE' or die "Cannot open 'OUTPUT_FILE' $!";


my $lines = 10;


while ( <> ) {


$. == 1 && print OUT "*****\n";


$. <= $lines && print OUT $_;


$. > $lines || eof and close ARGV;


}


__END__


I chose this one because it seems the likely candidate to extend for
what I would ideally like to do. However, Perl is so obscure to me now
that I may be wrong about that.

I would like to print the lines of each file that begin with an open
parenthesis '('. These are the comment lines in the beginning of each
file.

I originally chose the top ten lines because I thought that would
suffice, but upon reflection, I need only the lines that begin with
'('. Sometimes there are more than 10 comment lines.

Once the comment lines have stopped there are additional lines, but I
do not care about these, so I can go on to the next file.

Ideally, I would like to print the lines that begin with certain
headers beginning with a '(', such as '(CUSTOMER', or '(MATERIAL'.

So, read each line
Does it begin with '(' OR Does it begin with '(CUSTOMER'
If it Does Print the line to the Output File
 
A

A. Sinan Unur

Thank you all for your replies. I appreciate the time you have spent
on my problem.

There is no description of the problem the following code is supposed to
solve. Please quote an appropriate amount of context when you reply.
#!/usr/bin/perl
use warnings;
use strict;
Excellent!

open FILE, '<', 'text_file' or die "Cannot open 'text_file' $!";

These days, using lexical filehandles is preferred:

open my $file, '<', 'text_file' or die "Cannot open 'text_file' $!";

Good job checking if open succeeded.
chomp( @ARGV = <FILE> );

close FILE;

open OUT, '>>', 'OUTPUT_FILE' or die "Cannot open 'OUTPUT_FILE' $!";

my $lines = 10;

You might want to consider using a more expressive variable name.
while ( <> ) {
$. == 1 && print OUT "*****\n";
$. <= $lines && print OUT $_;
$. > $lines || eof and close ARGV;
}

While kinda cute, using logical operators for branching can easily get
out of hand. The block above can be better written as (untested code
follows):

print $out "*****\n" if $. == 1;

if ($. <= $lines_to_read) {
print;
next;
}

if( eof or ($. > $lines_to_read) ) {
close ARGV;
next;
}
I would like to print the lines of each file that begin with an open
parenthesis '('. These are the comment lines in the beginning of each
file.
....

Once the comment lines have stopped there are additional lines, but I
do not care about these, so I can go on to the next file.
....

Ideally, I would like to print the lines that begin with certain
headers beginning with a '(', such as '(CUSTOMER', or '(MATERIAL'.

So, read each line
Does it begin with '(' OR Does it begin with '(CUSTOMER'
If it Does Print the line to the Output File

You can write that in Perl and see if it works. If it does not, post
your code here.

Sinan
 
J

John W. Krahn

A. Sinan Unur said:
There is no description of the problem the following code is supposed to
solve. Please quote an appropriate amount of context when you reply.


Excellent!

Thank you, I try.

These days, using lexical filehandles is preferred:

open my $file, '<', 'text_file' or die "Cannot open 'text_file' $!";

Good job checking if open succeeded.

Thanks again. I like to set a good example.

You might want to consider using a more expressive variable name.

What would you suggest?

While kinda cute, using logical operators for branching can easily get
out of hand. The block above can be better written as (untested code
follows):

print $out "*****\n" if $. == 1;

if ($. <= $lines_to_read) {
print;
next;

You can't use next there because you might reach eof before $lines_to_read has
been reached. You should test your code before posting.

}

if( eof or ($. > $lines_to_read) ) {
close ARGV;
next;

Using next there is redundant as this is the end of the while loop.


Although why you didn't critique the code when I originally posted it?



John
 
A

A. Sinan Unur

A. Sinan Unur wrote:

Thank you, I try.

Well, maybe it wasn't obvious, but I did not realize that you had
written the script. The OP gave no indication that he had not written
the script, and if did not remember the earlier messages in the thread,
and I did not go back and read all the previous messages.
Thanks again. I like to set a good example.

I do appreciate the irony, but I was trying to be positive in a case
where I though the OP was following all the rules.
What would you suggest?
$lines_to_read
....
You should test your code before posting.

I should and I do normally. At least, I did warn I had not tested it.
Although why you didn't critique the code when I originally posted it?

John

See above.

Sinan
 
R

robic0

Sherm said:
What would be done any other way?


No, bizarro world is reading a random comment like yours, with no context and
no quoted material to give it meaning.
Sorry, I'll try it again.
This newsreader of mine has problems. I just posted this here and it
disapeared.
Been having problems with it, hope this is readable:
No, orrazib dlrow si gnidaer a modnar tnemmoc ekil sruoy, htiw on txetnoc dna
on detouq lairetam ot evig ti gninaem.
Tintin wrote:

#!/rsu/nib/lrep
esu sgninraw;
esu tcirts;

ym $tsilelif = "/htap/ot/40015002RS.txt";
ym $tuptuo = "/htap/ot/tuptuo.txt";

nepo TUPNI,$tsilelif ro eid "naC ton nepo $tsilelif $!\n";
nepo TUPTUO,">>$tuptuo" ro eid "naC ton nepo $tuptuo rof gnidneppa $!\n";

elihw (<TUPNI>) {
pmohc;
nepo ELIF, $_ ro eid "naC ton nepo $_ $!\n";

elihw (<ELIF>) {
tnirp TUPTUO;
tsal fi $. == 01;
}

esolc ELIF;
}
seY, yhw dluow siht eb enod yna rehto yaw.
@VGRA si orrazzib dlrow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top