Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need File I/O Help....

0 views
Skip to first unread message

RodFGWA

unread,
Oct 4, 2005, 4:02:21 PM10/4/05
to
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.

John Bokma

unread,
Oct 4, 2005, 4:33:42 PM10/4/05
to
"RodFGWA" <rodney...@gmail.com> wrote:

> 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);
^^^^^^^^^^^^

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)

John W. Krahn

unread,
Oct 4, 2005, 6:11:04 PM10/4/05
to
RodFGWA wrote:
> 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
--
use Perl;
program
fulfillment

Tintin

unread,
Oct 5, 2005, 3:55:46 AM10/5/05
to

"RodFGWA" <rodney...@gmail.com> wrote in message
news:1128456141.4...@z14g2000cwz.googlegroups.com...

#!/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;
}


RodFGWA

unread,
Oct 5, 2005, 7:46:11 AM10/5/05
to
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. Sinan Unur

unread,
Oct 5, 2005, 8:46:11 AM10/5/05
to
"RodFGWA" <rodney...@gmail.com> wrote in
news:1128512771.4...@g14g2000cwa.googlegroups.com:

> 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

--
A. Sinan Unur <1u...@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

John W. Krahn

unread,
Oct 5, 2005, 4:56:49 PM10/5/05
to
A. Sinan Unur wrote:
> "RodFGWA" <rodney...@gmail.com> wrote in
> news:1128512771.4...@g14g2000cwa.googlegroups.com:
>
>>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!

Thank you, I try.


>>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.

Thanks again. I like to set a good example.


>>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.

What would you suggest?


>>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;

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?

A. Sinan Unur

unread,
Oct 5, 2005, 6:19:30 PM10/5/05
to
"John W. Krahn" <som...@example.com> wrote in
news:l6X0f.1020$S4.528@edtnps84:

> A. Sinan Unur wrote:

>> "RodFGWA" <rodney...@gmail.com> wrote in
>> news:1128512771.4...@g14g2000cwa.googlegroups.com:
>>
>>>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!
>
> 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.

>> Good job checking if open succeeded.
>
> 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.

>>>my $lines = 10;
>>
>> You might want to consider using a more expressive variable name.
>
> What would you suggest?

$lines_to_read

>>>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;

...


> 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.

rob...@yahoo.com

unread,
Oct 5, 2005, 7:01:12 PM10/5/05
to
Yes, why would this be done any other way.
@ARGV is bizzarro world.

Sherm Pendley

unread,
Oct 5, 2005, 8:33:59 PM10/5/05
to
"rob...@yahoo.com" <rob...@yahoo.com> writes:

> Yes, why would this be done any other way.

What would be done any other way?

> @ARGV is bizzarro world.

No, bizarro world is reading a random comment like yours, with no context and
no quoted material to give it meaning.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

rob...@yahoo.com

unread,
Oct 5, 2005, 10:56:31 PM10/5/05
to

Sherm Pendley wrote:
> "rob...@yahoo.com" <rob...@yahoo.com> writes:
>
> > Yes, why would this be done any other way.
>
> What would be done any other way?
>
> > @ARGV is bizzarro world.
>
> 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.

Sherm Pendley

unread,
Oct 5, 2005, 11:16:23 PM10/5/05
to
"rob...@yahoo.com" <rob...@yahoo.com> writes:

> This newsreader of mine has problems.

You're not using a news reader, you're using a web browser. But yes, Google
Groups has *lots* of problems. You're one of them.

> 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.

*KNOLP*

Tad McClellan

unread,
Oct 5, 2005, 11:24:46 PM10/5/05
to
Sherm Pendley <sh...@dot-app.org> wrote:
> "rob...@yahoo.com" <rob...@yahoo.com> writes:

>> @ARGV is bizzarro world.
>
> No, bizarro world is reading a random comment like yours, with no context and
> no quoted material to give it meaning.


It is not bizzarro, it is just more uninspired trolling.

Message-ID: <k66q4154rmcigtunm...@4ax.com>


--
Tad McClellan (God, according to some)
ta...@augustmail.com
Fort Worth, Texas

rob...@yahoo.com

unread,
Oct 6, 2005, 12:56:48 AM10/6/05
to
On Wed, 05 Oct 2005 23:16:23 -0400, Sherm Pendley <sh...@dot-app.org>
wrote:

>"rob...@yahoo.com" <rob...@yahoo.com> writes:
>
>> sihT redaerswen fo enim sah smelborp.
>
>uoY'er ton gnisu a swen redaer, uoy'er gnisu a bew resworb. tuB sey, elgooG
>spuorG sah *stol* fo smelborp. uoY'er eno fo meht.
>
>> neeB gnivah smelborp htiw ti, epoh siht si elbadaer:
>>
>>> oN, bizarro world is reading a random comment like yours, with no context


>>> and no quoted material to give it meaning.
>

>*PLONK*
>
>mrehs--

huh?

rob...@yahoo.com

unread,
Oct 6, 2005, 1:03:16 AM10/6/05
to
On Wed, 5 Oct 2005 22:24:46 -0500, Tad McClellan
<ta...@augustmail.com> wrote:

>tI si ton orrazzib, ti si tsuj erom deripsninu gnillort.
>
> egasseM-DI: <xa55km6dai7gij7nhnvmnutgicmr4514q66k.moc>
>
>
>--
> daT nallelCcM (doG, gnidrocca ot emos)
> cmdat.moc
> troF htroW, saxeT
what?

rob...@yahoo.com

unread,
Oct 22, 2005, 3:34:25 AM10/22/05
to
On Wed, 5 Oct 2005 22:24:46 -0500, Tad McClellan
<ta...@augustmail.com> wrote:
Tad: "God, according to some"
Hey Tad, are you GOD?
Bells ringing, back to class!

Jürgen Exner

unread,
Oct 22, 2005, 9:30:12 AM10/22/05
to
rob...@yahoo.com wrote:
> Bells ringing, back to class!

Shouldn't that be "Bells are ringing, it is Christmas time?"

jue


0 new messages