Need File I/O Help....

Discussion in 'Perl Misc' started by RodFGWA, Oct 4, 2005.

  1. RodFGWA

    RodFGWA Guest

    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.
     
    RodFGWA, Oct 4, 2005
    #1
    1. Advertising

  2. RodFGWA

    John Bokma Guest

    "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.
    >
    > 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 Bokma, Oct 4, 2005
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, Oct 4, 2005
    #3
  4. RodFGWA

    Tintin Guest

    "RodFGWA" <> wrote in message
    news:...
    >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;
    }
     
    Tintin, Oct 5, 2005
    #4
  5. RodFGWA

    RodFGWA Guest

    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
     
    RodFGWA, Oct 5, 2005
    #5
  6. "RodFGWA" <> wrote in
    news::

    > 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 <>
    (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
     
    A. Sinan Unur, Oct 5, 2005
    #6
  7. A. Sinan Unur wrote:
    > "RodFGWA" <> wrote in
    > news::
    >
    >>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?



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Oct 5, 2005
    #7
  8. "John W. Krahn" <> wrote in
    news:l6X0f.1020$S4.528@edtnps84:

    > A. Sinan Unur wrote:


    >> "RodFGWA" <> wrote in
    >> news::
    >>
    >>>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.

    Sinan

    --
    A. Sinan Unur <>
    (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
     
    A. Sinan Unur, Oct 5, 2005
    #8
  9. RodFGWA

    Guest

    Yes, why would this be done any other way.
    @ARGV is bizzarro world.
     
    , Oct 6, 2005
    #9
  10. "" <> 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
     
    Sherm Pendley, Oct 6, 2005
    #10
  11. RodFGWA

    Guest

    Sherm Pendley wrote:
    > "" <> 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.
     
    , Oct 6, 2005
    #11
  12. "" <> 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*

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Oct 6, 2005
    #12
  13. Sherm Pendley <> wrote:
    > "" <> 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: <>


    --
    Tad McClellan (God, according to some)

    Fort Worth, Texas
     
    Tad McClellan, Oct 6, 2005
    #13
  14. RodFGWA

    Guest

    On Wed, 05 Oct 2005 23:16:23 -0400, Sherm Pendley <>
    wrote:

    >"" <> 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?
     
    , Oct 6, 2005
    #14
  15. RodFGWA

    Guest

    On Wed, 5 Oct 2005 22:24:46 -0500, Tad McClellan
    <> 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?
     
    , Oct 6, 2005
    #15
  16. RodFGWA

    Guest

    On Wed, 5 Oct 2005 22:24:46 -0500, Tad McClellan
    <> wrote:
    Tad: "God, according to some"
    Hey Tad, are you GOD?
    Bells ringing, back to class!

    >Sherm Pendley <> wrote:
    >> "" <> 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: <>
     
    , Oct 22, 2005
    #16
  17. wrote:
    > Bells ringing, back to class!


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

    jue
     
    Jürgen Exner, Oct 22, 2005
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,475
    Edward
    Jun 18, 2004
  2. Rahul
    Replies:
    0
    Views:
    1,032
    Rahul
    Nov 28, 2006
  3. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    567
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  4. ElementX
    Replies:
    9
    Views:
    494
    RedGrittyBrick
    Oct 1, 2008
  5. Replies:
    5
    Views:
    85
    Chris Angelico
    May 14, 2014
Loading...

Share This Page