For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2006 > Better way to add one line at top of every file(one liner)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Better way to add one line at top of every file(one liner)
Ankur Gupta

2006-05-19, 3:58 am

perl -0777 -p -i -e 'print "abcdefgh\n"' *.ext
or
perl -0777 -p -i -e 's#^#abcdefgh\n#' *.ext
or
???

--Ankur
Uri Guttman

2006-05-19, 3:58 am

>>>>> "AG" == Ankur Gupta <ankur2080@gmail.com> writes:

AG> perl -0777 -p -i -e 'print "abcdefgh\n"' *.ext
AG> or
AG> perl -0777 -p -i -e 's#^#abcdefgh\n#' *.ext
AG> or
AG> ???

a new sub called prepend_file is planned be added to file::slurp that
will do exactly that. so it may potentially look like this for a one
liner:

perl -MFile::Slurp=prepend_file \
-e 'prepend_file( $_, "abcdefgh\n" ) for @ARGV' *.ext

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Wolf

2006-05-19, 3:58 am

I would suggest you go with the second. Also see s modifier of the s///
operator. However since you have not specified the g modifier it should
not matter.

Also you could try sed.

Xicheng Jia

2006-05-19, 3:58 am

Ankur Gupta wrote:
> perl -0777 -p -i -e 'print "abcdefgh\n"' *.ext
> or
> perl -0777 -p -i -e 's#^#abcdefgh\n#' *.ext
> or
> ???


perl -i -lpe 'INIT{print "abcdefgh"}' *.ext

XC

Xicheng Jia

2006-05-19, 3:58 am

Xicheng Jia wrote:
> Ankur Gupta wrote:
>

=> perl -i -lpe 'INIT{print "abcdefgh"}' *.ext

sorry, this is only useful for a single file...:-(

XC

David Romano

2006-05-19, 6:58 pm

Hi Ankur,
On 5/18/06, Ankur Gupta wrote:
> perl -0777 -p -i -e 'print "abcdefgh\n"' *.ext
> or
> perl -0777 -p -i -e 's#^#abcdefgh\n#' *.ext
> or
> ???

I did this:
perl -p -i -e '$_ =3D ($ARGV ne $f && $f =3D $ARGV) ? "NEW FIRST LINE\n$_"
: $_' *.ext

I needed to check if I'm on the first line of a file and only change
$_ if I am. When using -p, the eval code is placed in "while (<> ) {
print;}" block right before the print function:

while (<> ) {
$_ =3D ($ARGV ne $f && $f =3D $ARGV) ? "NEW FIRST LINE\n$_" : $_;
print;
}

So what I did is use $f to signify the name of the file that the
previous line was in. I check to see what the name of the current file
is ($ARGV), and if they are different then I know I've moved to a new
file. So I set $f to the name of the file the current line is in: $f =3D
$ARGV. (The && is short-circuited and so the assignment will only
happen if $ARGV is not equal to $f.) I use the tertiary operator (?:)
to prefix $_ with whatever content I want if it's the first line in
the file, or nothing it's not.

HTH,
David
Luke Bakken

2006-05-19, 6:58 pm

On 5/19/06, David Romano <david.romano@gmail.com> wrote:
> Hi Ankur,
> On 5/18/06, Ankur Gupta wrote:
> I did this:
> perl -p -i -e '$_ =3D ($ARGV ne $f && $f =3D $ARGV) ? "NEW FIRST LINE\n$_=

"
> : $_' *.ext


This'll (efficiently) add "FOO BAR BAZ BAT" to the beginning of INPUT.TXT

perl -MTie::File -e"tie @a, q(Tie::File),$ARGV[1] or die;unshift
@a,$ARGV[0];untie @a" "FOO BAR BAZ BAT" INPUT.TXT

Doing this to multiple files is left as an exercise in find and xargs
(or -exec '{}' +)
David Romano

2006-05-19, 6:58 pm

On 5/19/06, Luke Bakken <luke.bakken@gmail.com> wrote:
> On 5/19/06, David Romano <david.romano@gmail.com> wrote:
$_"[color=darkred]
>
> This'll (efficiently) add "FOO BAR BAZ BAT" to the beginning of INPUT.TXT
>
> perl -MTie::File -e"tie @a, q(Tie::File),$ARGV[1] or die;unshift
> @a,$ARGV[0];untie @a" "FOO BAR BAZ BAT" INPUT.TXT

Oh yeah! I forgot about Tie::File for some reason. :-)

David
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com