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 '$_ = ($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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to