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

Hi Monks,

I am having a very big file around 100 lines. I have to create a file dumping these 100 lines into it.

I know the concept of writing a line-

 "print MYFILE "abc, def";

I cant repeat the above statement for 100 of my lines! Any easy way?

Thanks in Advance

Replies are listed 'Best First'.
Re: Writing many lines to a file
by cavac (Prior) on Jul 16, 2012 at 13:17 UTC

    First of all, 100 lines is a small file, not a big one ;-)

    If you just need to copy the file and do something to every line, this might be something similar to what you want (untested, written from memory):

    #!/usr/bin/env perl use strict; use warnings; open(my $ifh, '<', 'infile.txt') or die($!); open(my $ofh, '>', 'outfile.txt') or die($!); while((my $line = <$ifh>)) { chomp $line; # do something to $line print $ofh $line, "\n"; } close($ifh); close($ofh);

    If you really just want to copy a file, you can use File::Copy:

    #!/usr/bin/env perl use strict; use warnings; use File::Copy; copy('infile.txt', 'outfile.txt') or die("Ooops, i messed up: $!");

    "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"
Re: Writing many lines to a file
by gri6507 (Deacon) on Jul 16, 2012 at 12:47 UTC
    It looks like you are just starting with Perl. One of the key features of Perl is that There Is More That One Way To Do It (TIMTOWTDI). Depending on the exact problem you are trying to solve, the solution may be different. However, given what little background you have specified in your post, one easy way to accomplish what you want is with the following one line

    print MYFILE "abc, def\n" for (1 .. 100);

    I rather suspect that your actual problem may be somewhat different, so please post any follow up questions here, after reading about How do I post a question effectively?

Re: Writing many lines to a file
by daxim (Curate) on Jul 16, 2012 at 12:44 UTC
    I'm asking for clarification: can't you just copy the file? Does the file consist of 100 lines which are the same?
Re: Writing many lines to a file
by aitap (Curate) on Jul 16, 2012 at 13:23 UTC
    Another method to write 100 lines: print FILE "line\n"x100;
    Sorry if my advice was wrong.
Re: Writing many lines to a file
by uday_sagar (Scribe) on Jul 16, 2012 at 13:10 UTC

    Daxim,

    The twist is- I shouldnt copy that file, but put the contents of the file in perl program itself.

    gri,

    I have different lines, not the same one to be repeated.

      Ok, what you are saying is, you want to include that data within the perl script and write it out as a text file, correct?

      First approach is to copy the data into the __DATA__ segment of the script:

      #!/usr/bin/env perl use strict; use warnings; open(my $ofh, '>', 'outfile.txt') or die($!); while((my $line = <DATA>)) { print $ofh $line; } close($ofh); __DATA__ Line 1 Line 2 Line 3 Line 4

      Or, you can use HEREDOCs

      #!/usr/bin/env perl use strict; use warnings; open(my $ofh, '>', 'outfile.txt') or die($!); print $ofh <<ENDFILE; Line 1 Line 2 Line 3 Line 4 ENDFILE close($ofh);

      "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"
Re: Writing many lines to a file
by uday_sagar (Scribe) on Jul 16, 2012 at 13:37 UTC
    You got me cavac! Thanks for the quick reply.

      :-)

      BTW, this isn't ye oldy forum software. If you reply to a post, use the reply link on that post, so the threads are shown correctly. Also, the author you are replying to gets an automated message. Thanks.

      "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"
Re: Writing many lines to a file
by pvaldes (Chaplain) on Jul 16, 2012 at 21:42 UTC
    perl -ne 'print if $. <= 100' infile.txt > outfile.txt