[question: cannot delete the last line of a file] 
Author Message
 [question: cannot delete the last line of a file]

Hi,

I have a couple questions and like your help in them:

1) Can I source a data file in PERL just like in C/Bourne Shell?
If so, how?

2) I couldn't remove the last line in a file, below are my codes.
Please tell me what's wrong, or a better way:

#!/bin/perl5
$datafile = "datafile";

# The section deletes last line of file
if (-e $logfile)
{
    system("sed '$d' $datafile > logfile.1");

Quote:
}

Thanks much in advance.

PL



Wed, 19 Jan 2005 08:27:45 GMT  
 [question: cannot delete the last line of a file]

Quote:

> I have a couple questions and like your help in them:

> 1) Can I source a data file in PERL just like in C/Bourne Shell?
> If so, how?

I don't think so.  What exactly do you mean by "source a data file"?

Quote:
> 2) I couldn't remove the last line in a file, below are my codes.
> Please tell me what's wrong, or a better way:

> #!/bin/perl5
> $datafile = "datafile";

> # The section deletes last line of file
> if (-e $logfile)
> {
>     system("sed '$d' $datafile > logfile.1");
> }

#!/bin/perl5 -w
use strict;


while ( <> ) {
    exit if eof;
    print;
    }

__END__

John
--
use Perl;
program
fulfillment



Thu, 20 Jan 2005 19:21:36 GMT  
 [question: cannot delete the last line of a file]

Quote:


> > 1) Can I source a data file in PERL just like in C/Bourne Shell?
> > If so, how?

> I don't think so.  What exactly do you mean by "source a data file"?

If your "data file" contains valid Perl, you need the do command:

  do "filename";

If it contains C or Bourne shell commands, check out Shell::Source on
CPAN.

--

http://www.pjcj.net



Thu, 20 Jan 2005 21:48:57 GMT  
 [question: cannot delete the last line of a file]

Quote:

> 1) Can I source a data file in PERL just like in C/Bourne Shell?

No.

Quote:
> If so, how?

If you want the source'd file to be active in a single "session",
then you can chain them together in the same system() call:

   system '. stuff.env;  prog-that-uses-env';

If you want environment variables set, you can do that directly
from within Perl by accessing the %ENV hash.

Quote:
> 2) I couldn't remove the last line in a file, below are my codes.
> Please tell me what's wrong, or a better way:

                 ^^^^^^^^^^^^

1) shells out to sed instead of doing it in native Perl.

2) warnings not enabled

3) strict not enabled

Quote:
> #!/bin/perl5

   #!/bin/perl5
   use warnings;
   use strict;

You can delete the last line from native Perl:


   open LOG, ">$logfile" or die "could not open '$logfile'  $!";
   while ( <> ) {
      print LOG unless eof;
   }

Quote:
}

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas


Fri, 21 Jan 2005 00:52:23 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. AsString Return different value in ADOQuery and TQuery for char field

2. how to delete last line in .txt.file

3. Deleting and or appending a single line in a text file - newbie question

4. Cannot delete temp file using system() cmd

5. BUG ?=>Cannot delete file with name more than 19 character

6. BDE Error $000F

7. Unable to allocate sufficient memory (SQL Links 2.52)

8. Need routine for printing the screen in graphics mode

9. How to unlink (delete) last file ?

10. Reading Last X Lines of File?

11. How to get last line in ASCII file?

12. trim the last blank-line and compare files

 

 
Powered by phpBB® Forum Software