Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > use one line Perl command to add a line at the begin of a file

Reply
Thread Tools

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

 
 
Ting Wang
Guest
Posts: n/a
 
      12-13-2005
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
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      12-13-2005
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

 
Reply With Quote
 
 
 
 
Ting Wang
Guest
Posts: n/a
 
      12-13-2005
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.
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      12-13-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl format statement, how do I begin a line with a hash mark? joinerda Perl Misc 5 05-15-2010 12:22 PM
command-line one-liners a la Perl? kj Python 2 06-18-2009 02:32 PM
Pull out only first field of ps command ( in Perl ) PERL RUN COMMAND tweetiebirds@gmail.com Perl Misc 2 03-26-2005 06:03 AM
A question about Perl: using perl command line to replace strings... lucy Perl Misc 7 09-03-2004 07:57 AM
Perl command to copy one file into another file? Bill Perl Misc 8 09-30-2003 11:11 PM



Advertisments