Code Comments
Programming Forum and web based access to our favorite programming groups.perl -0777 -p -i -e 'print "abcdefgh\n"' *.ext or perl -0777 -p -i -e 's#^#abcdefgh\n#' *.ext or ??? --Ankur
Post Follow-up to this message>>>>> "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 -------- [url]http://www.stemsystems.com[/url ] --Perl Consulting, Stem Development, Systems Architecture, Design and Coding - Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url ]
Post Follow-up to this messageI 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.
Post Follow-up to this messageAnkur 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
Post Follow-up to this messageXicheng Jia wrote:
> Ankur Gupta wrote:
>
=> perl -i -lpe 'INIT{print "abcdefgh"}' *.ext
sorry, this is only useful for a single file...:-(
XC
Post Follow-up to this messageHi 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
Post Follow-up to this messageOn 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 '{}' +)
Post Follow-up to this messageOn 5/19/06, Luke Bakken <luke.bakken@gmail.com> wrote: > On 5/19/06, David Romano <david.romano@gmail.com> wrote: $_" > > 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.