Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I am new to perl and am working on a text file , now I wish to copy a part of text file to some other text file . So what I am doing is breaking it into words and printing it on a new file , but it destroys the format of file

The good thing is that I know that where I have to stop coping which is as soon as I find the word "data list ".

So please help

Replies are listed 'Best First'.
Re: Coping a part of text file
by davido (Cardinal) on Jul 04, 2013 at 08:02 UTC

    Please help us help you:

    • Post sample input.
    • Post the output your existing code produces given the sample input.
    • Post 5 to 25 lines of code that is capable of reproducing this output.
    • Post the sample output you actually desire.

    Dave

      My file looks something like this
      FILENAME: tes.txt FileName: + Function: + Written by: + + REVISION -Rev. -- Date -------------------------------------------------- 00 11/11/11 Initial release. 01 12/04/12 Edited 01 12/04/12 Final 02 01/10/13 Sispatched ----------------------------------------------------------------*/ DATA LIST /*========= List =========*/ Currency ............. cotd

      the output I desire is this

      FILENAME: tes.txt FileName: + Function: + Written by: + + REVISION -Rev. -- Date -------------------------------------------------- 00 11/11/11 Initial release. 01 12/04/12 Edited 01 12/04/12 Final 02 01/10/13 Sispatched ----------------------------------------------------------------*/ DATA LIST

      what I am getting is all the words in a same line

        perl -pe '/========= List =========/ and exit;' infile > outfile

        ... or is the requirement more complex than specified?

        Where's the code?

        Update: Nevermind, I see you've posted it in response to another follow-up in this thread.


        Dave

Re: Coping a part of text file
by Anonymous Monk on Jul 04, 2013 at 07:59 UTC
      sub To_header { my $ff; my $iz; open($ff,">hr.txt"); for ($iz=0;$iz<=$temp[0];$iz++) { print $ff "$words[$iz]" ; } }

      here words is an array of words of file and temp[0] has the address till where I wish to print and I am printing it in hr.txt

        Ok, here is my commentary

        sub To_header { my( $outfile, $startix, $endix , $words ) = @_; use autodie; open my($outfh), '>', $outfile ; ## or die by autodie for my $ix ( $startix .. $endix ){ print $outfh $words->[ $ix ]; } close $outfh; } To_header( 'hr.txt', 0, $temp[0], \@words );

        If you want something more specific you'll have to provide more details as How do I post a question effectively? and davido explain

Re: Coping a part of text file
by BillKSmith (Monsignor) on Jul 04, 2013 at 15:34 UTC
    use strict; use warnings; while (<DATA>) { print if /FileName/ .. /DATA LIST/; } __DATA__ FileName: + Function: + Written by: + + REVISION -Rev. -- Date -------------------------------------------------- 00 11/11/11 Initial release. 01 12/04/12 Edited 01 12/04/12 Final 02 01/10/13 Sispatched ----------------------------------------------------------------*/ DATA LIST /*========= List =========*/ Currency ............. cotd
    Bill
Re: Coping a part of text file
by mtmcc (Hermit) on Jul 05, 2013 at 12:12 UTC

    How about this?

    #!/usr/bin/perl use strict; open (INPUT, "<", "inputfile.txt"); open (OUTPUT, ">", "outputfile.txt"); while (<INPUT>) { print OUTPUT "$_"; last if $_ =~ m/DATA LIST/; } close INPUT; close OUTPUT;

    Hope you find your answer...

    Michael