Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Better way to add one line at top of every file(one liner)
perl -0777 -p -i -e 'print "abcdefgh\n"' *.ext
or
perl -0777 -p -i -e 's#^#abcdefgh\n#'  *.ext
or
???

--Ankur

Report this thread to moderator Post Follow-up to this message
Old Post
Ankur Gupta
05-19-06 08:58 AM


Re: Better way to add one line at top of every file(one liner)
>>>>> "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
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
05-19-06 08:58 AM


Re: Better way to add one line at top of every file(one liner)
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Wolf
05-19-06 08:58 AM


Re: Better way to add one line at top of every file(one liner)
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


Report this thread to moderator Post Follow-up to this message
Old Post
Xicheng Jia
05-19-06 08:58 AM


Re: Better way to add one line at top of every file(one liner)
Xicheng Jia wrote:
> Ankur Gupta wrote: 
>
=> perl -i -lpe 'INIT{print "abcdefgh"}' *.ext

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

XC


Report this thread to moderator Post Follow-up to this message
Old Post
Xicheng Jia
05-19-06 08:58 AM


Re: Better way to add one line at top of every file(one liner)
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

Report this thread to moderator Post Follow-up to this message
Old Post
David Romano
05-19-06 11:58 PM


Re: Better way to add one line at top of every file(one liner)
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 '{}' +)

Report this thread to moderator Post Follow-up to this message
Old Post
Luke Bakken
05-19-06 11:58 PM


Re: Better way to add one line at top of every file(one liner)
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
David Romano
05-19-06 11:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:09 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.