Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    1

    Unanswered: Using perl to add a line at the end of file

    I have a question, how to use perl to add a line at the end of a file?
    I have tried this command: perl -i~ -0777pe'/s/$/\nmytext/' myfile
    but this command add two same lines to the file.
    Could anyone help me?

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    I wouldn't know how to add a line using perl but here's how you could do it in awk...

    awk '{print}END{print "yourText"}' yourFile

    I can run 'a2p' on this, which converts awk to perl, and you get...

    -----------------------------------------------------------
    #!/usr/opt/perl5/bin/perl
    eval 'exec /usr/opt/perl5/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
    # this emulates #! processing on NIH machines.
    # (remove #! line above if indigestible)

    eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
    # process any FOO=bar switches

    $, = ' '; # set output field separator
    $\ = "\n"; # set output record separator

    while (<>) {
    chomp; # strip record separator
    print $_;
    }

    print 'yourText';
    ------------------------------------------------------------

    I don't know much perl but this looks like overkill to me!

    I'd just use echo...

    echo "yourtext" >> yourFile

    HTH

  3. #3
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650

    Re: Using perl to add a line at the end of file

    Perl Questions are being answered in a different forum ... Try posting your question there

    Sathyaram
    Visit the new-look IDUG Website , register to gain access to the excellent content.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •