use one line Perl command to add a line at the begin of a file

Discussion in 'Perl Misc' started by Ting Wang, Dec 13, 2005.

  1. Ting Wang

    Ting Wang Guest

    Hallo all,
    I wanted to add a line (e.g #include "some.h") at the begin of all .c files
    in a directory.
    I try to change one .c file with one line Perl command

    perl -i.org -p -e 'BEGIN{print "#include \"some.h\"\n";}'

    But this doesn't work, "#include "some.h"" will be printed into STDOUT.
    I did it with a 'long' Perl script:

    my $f = shift;
    my $fm = $f.".org";
    `touch $fm`;
    open F, $f or die "can not open file $f $!";
    open FM, "> $fm" or die "can not open file $fm $!";
    select FM;
    print "#include \"some.h\"\n";
    while (<F>)
    {
    print;
    }
    close F;
    close FM;
    `mv $fm tmp`;
    `mv $f $fm`;
    `mv tmp $f`;

    Is there another way to do it (a one line Perl command)?


    Thanks alot

    Ting
     
    Ting Wang, Dec 13, 2005
    #1
    1. Advertising

  2. Ting Wang

    Paul Lalli Guest

    Ting Wang wrote:

    > I wanted to add a line (e.g #include "some.h") at the begin of all .c files
    > in a directory.
    > I try to change one .c file with one line Perl command
    >
    > perl -i.org -p -e 'BEGIN{print "#include \"some.h\"\n";}'
    >
    > But this doesn't work, "#include "some.h"" will be printed into STDOUT.


    > Is there another way to do it (a one line Perl command)?


    See the documentation for -i in perldoc perlrun, eof in perldoc -f eof,
    and $. in perldoc perlvar:

    perl -ni.org -e'print qq{#include "some.h"\n} if $. == 1; print; close
    ARGV if eof;' *.c

    Paul Lalli
     
    Paul Lalli, Dec 13, 2005
    #2
    1. Advertising

  3. Ting Wang

    Ting Wang Guest

    Thanks alot for your help.
    $. is exactly what i want :).
    And i found
    perl -i.org -p -e'print qq{#include "some.h"\n} if $. == 1;'
    works too.
     
    Ting Wang, Dec 13, 2005
    #3
  4. Ting Wang

    Paul Lalli Guest

    Ting Wang wrote:
    > Thanks alot for your help.
    > $. is exactly what i want :).
    > And i found
    > perl -i.org -p -e'print qq{#include "some.h"\n} if $. == 1;'
    > works too.


    That will work if and only if you have exactly one file. Your original
    email stated you wanted to work on *all* .c files in the directory.
    This is why I told you to read up on
    perldoc -f eof

    Paul Lalli
     
    Paul Lalli, Dec 13, 2005
    #4
    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. kj
    Replies:
    2
    Views:
    419
    unayok
    Jun 18, 2009
  2. Jon Redgrave

    One line command line filter

    Jon Redgrave, Sep 5, 2011, in forum: Python
    Replies:
    9
    Views:
    379
    Hans Mulder
    Sep 6, 2011
  3. Bill
    Replies:
    8
    Views:
    2,341
    Jay Tilton
    Oct 1, 2003
  4. Replies:
    2
    Views:
    165
    Tad McClellan
    Mar 26, 2005
  5. joinerda
    Replies:
    5
    Views:
    192
    J├╝rgen Exner
    May 15, 2010
Loading...

Share This Page