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

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

Post by ankur208 » Sat, 20 May 2006 14:59:00


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

--Ankur
 
 
 

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

Post by Uri Guttma » Sat, 20 May 2006 15:49:30

>>>>> "AG" == Ankur Gupta < XXXX@XXXXX.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 ------ XXXX@XXXXX.COM -------- http://www.yqcomputer.com/
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://www.yqcomputer.com/

 
 
 

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

Post by Wolf » Sat, 20 May 2006 15:51:32

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.
 
 
 

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

Post by Xicheng Ji » Sat, 20 May 2006 16:20:56


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

XC
 
 
 

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

Post by Xicheng Ji » Sat, 20 May 2006 16:23:37


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

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

XC
 
 
 

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

Post by david.roma » Sun, 21 May 2006 01:36:13

Hi Ankur,

I did this:
perl -p -i -e '$_ = ($ARGV ne $f && $f = $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 (<>) {
$_ = ($ARGV ne $f && $f = $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 =
$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
 
 
 

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

Post by luke.bakke » Sun, 21 May 2006 03:03:59


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 '{}' +)
 
 
 

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

Post by david.roma » Sun, 21 May 2006 07:20:22


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

David