Re: translate following files based on the last line of the previous file

2012-04-23 Thread lina

On Monday 23,April,2012 09:28 PM, lina wrote:

On Monday 23,April,2012 01:27 AM, Jim Gibson wrote:


On Apr 22, 2012, at 9:52 AM, lina wrote:

Here is what I Have came up so far,


#!/usr/bin/env perl

use strict;
use warnings;
use autodie qw(open close);
use 5.012;

my $dict = system("tail -n 1 text_1.xvg");


Read the documentation on the system function. It does not return the
output of the child process to your program. For that, you need the
qx() operator, or backticks:

my $dict = qx("tail -n 1 text_1.xvg");


print $dict;
print "\n\n";
open my $fh, "<","text_2.xvg";
while(<$fh>){
print $_;
}


Of course, it is possible to read the last line of a file without
resorting to creating a child process. See, for example, the module
File::ReadBackwards.



Thanks.

Here I came up a working script (unavoidably clumsy).
I don't know how to refine it, or make it terse.

Thanks ahead for your time,


#!/usr/bin/env perl

use strict;
use warnings;
use autodie qw(open close);
use 5.012;

open my $fh1, "<","text_1.xvg";

my $lastline;
my %dict;
my @lastline;

my $n;

while(<$fh1>){
$lastline = $_ if eof;
}

@lastline = split /[\t ]/,$lastline;
$n = shift @lastline;

foreach my $i (0..3){
$dict{$i}=$lastline[$i]
}



open my $fh, "<","text_2.xvg";

my @old;

while(<$fh>){
$n+=2;
my @old = split /\s*/,$_;
shift @old;
foreach my $item (@old){
s/$item/$dict{$item}/g;
}
print $n, "\t", join " ", @old;
print "\n";
}



$ more text_1.xvg
0 0 1 2 3
2 1 0 2 3
4 1 2 0 3



$ more text_2.xvg
0 0 1 2 3
2 1 0 3 2
4 1 3 0 2



output
$ ./renumber_v1.pl
6 0 1 2 3
8 1 0 3 2
10 1 3 0 2


Best regards,


I started to realize that script not working.
It doesn't do the translation work.

What's wrong with the
 my @old = split /\s+/,$_;
 shift @old;
 foreach my $item (@old){
 s/$item/$dict{$item}/g;
 }

Thanks for pointing out why it failed to work.

Best regards,

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: translate following files based on the last line of the previous file

2012-04-23 Thread lina

On Monday 23,April,2012 01:27 AM, Jim Gibson wrote:


On Apr 22, 2012, at 9:52 AM, lina wrote:

Here is what I Have came up so far,


#!/usr/bin/env perl

use strict;
use warnings;
use autodie qw(open close);
use 5.012;

my $dict = system("tail -n 1 text_1.xvg");


Read the documentation on the system function. It does not return the output of 
the child process to your program. For that, you need the qx() operator, or 
backticks:

my $dict = qx("tail -n 1 text_1.xvg");


print $dict;
print "\n\n";
open my $fh, "<","text_2.xvg";
while(<$fh>){
    print $_;
}


Of course, it is possible to read the last line of a file without resorting to 
creating a child process. See, for example, the module File::ReadBackwards.



Thanks.

Here I came up a working script (unavoidably clumsy).
I don't know how to refine it, or make it terse.

Thanks ahead for your time,


#!/usr/bin/env perl

use strict;
use warnings;
use autodie qw(open close);
use 5.012;

open my $fh1, "<","text_1.xvg";

my $lastline;
my %dict;
my @lastline;

my $n;

while(<$fh1>){
$lastline = $_ if eof;
}

@lastline = split /[\t ]/,$lastline;
$n = shift @lastline;

foreach my $i (0..3){
$dict{$i}=$lastline[$i]
}



open my $fh, "<","text_2.xvg";

my @old;

while(<$fh>){
$n+=2;
my @old = split /\s*/,$_;
shift @old;
foreach my $item (@old){
s/$item/$dict{$item}/g;
}
print $n, "\t", join " ", @old;
print "\n";
}



$ more text_1.xvg
0   0 1 2 3
2   1 0 2 3
4   1 2 0 3



$ more text_2.xvg
0   0 1 2 3
2   1 0 3 2
4   1 3 0 2



output
$ ./renumber_v1.pl
6   0 1 2 3
8   1 0 3 2
10  1 3 0 2


Best regards,

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: translate following files based on the last line of the previous file

2012-04-22 Thread Jim Gibson

On Apr 22, 2012, at 9:52 AM, lina wrote:

Here is what I Have came up so far,
> 
> #!/usr/bin/env perl
> 
> use strict;
> use warnings;
> use autodie qw(open close);
> use 5.012;
> 
> my $dict = system("tail -n 1 text_1.xvg");

Read the documentation on the system function. It does not return the output of 
the child process to your program. For that, you need the qx() operator, or 
backticks:

my $dict = qx("tail -n 1 text_1.xvg");

> print $dict;
> print "\n\n";
> open my $fh, "<","text_2.xvg";
> while(<$fh>){
>   print $_;
> }

Of course, it is possible to read the last line of a file without resorting to 
creating a child process. See, for example, the module File::ReadBackwards.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




translate following files based on the last line of the previous file

2012-04-22 Thread lina

Hi,

I have a series of files.

$ cat text_1.xvg
0   0 1 2 3
2   1 0 2 3
4   1 2 0 3


$ cat text_2.xvg
0   0 1 2 3
2   1 0 3 2
4   1 3 0 2

I wish to translate the text_2 numbers (except the first field) based on 
the last line of the text_1.xvg


namely text_2.xvg will become:

0   1 2 0 3
2   2 1 3 0


Here is what I Have came up so far,



#!/usr/bin/env perl

use strict;
use warnings;
use autodie qw(open close);
use 5.012;

my $dict = system("tail -n 1 text_1.xvg");

print $dict;

print "\n\n";

open my $fh, "<","text_2.xvg";

while(<$fh>){
print $_;

}



./re_number.pl
4   1 2 0 3
0   ## but the last line got an extra 0,

0   0 1 2 3
2   1 0 3 2
4   1 3 0 2


Thanks ahead for the suggestions,

Best regards,

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: read the time from last line of a file in unix

2008-09-10 Thread Mr. Shawn H. Corey
On Tue, 2008-09-09 at 22:44 -0700, John W. Krahn wrote:
> Rodrick Brown wrote:
> > On Tue, Sep 9, 2008 at 3:23 PM,  <[EMAIL PROTECTED]> wrote:
> >>
> >> i want get the time specified in last line of the file.
> >> For eg:
> >> $tail -2 test.log
> >> 2008 aug 25 14:48:42.800 Sending ping message;
> >> 2008 aug 25 14:48:43.390 Sending ping message;
> >> The file size is huge, so i dont want to read the entire file to get
> >> the last line. Is there any way to get the last line of a file
> >> directly in perl? i need to get the hour(14) and min(48) specified in
> >> the last line of the file. can anybody help me on this?
> > 
> > You can mimic tail -1 using the following:
> > 
> > perl -nle 'push @l,$_;  END { print $l[$#l] }' /tmp/filename
> 
> You don't have to save the entire file in an array just to capture the 
> last line:
> 
> perl -ne'$l = $_; END { print $l }' /tmp/filename
> 
> Or even:
> 
> perl -pe'$\=$_}{' /tmp/filename
> 

The OP did ask NOT to read the entire file.


-- 
Just my 0.0002 million dollars worth,
  Shawn

"Where there's duct tape, there's hope."
Cross Time Cafe

"Perl is the duct tape of the Internet."
Hassan Schroeder, Sun's first webmaster


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




Re: read the time from last line of a file in unix

2008-09-09 Thread John W. Krahn

Rodrick Brown wrote:

On Tue, Sep 9, 2008 at 3:23 PM,  <[EMAIL PROTECTED]> wrote:


i want get the time specified in last line of the file.
For eg:
$tail -2 test.log
2008 aug 25 14:48:42.800 Sending ping message;
2008 aug 25 14:48:43.390 Sending ping message;
The file size is huge, so i dont want to read the entire file to get
the last line. Is there any way to get the last line of a file
directly in perl? i need to get the hour(14) and min(48) specified in
the last line of the file. can anybody help me on this?


You can mimic tail -1 using the following:

perl -nle 'push @l,$_;  END { print $l[$#l] }' /tmp/filename


You don't have to save the entire file in an array just to capture the 
last line:


perl -ne'$l = $_; END { print $l }' /tmp/filename

Or even:

perl -pe'$\=$_}{' /tmp/filename



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: read the time from last line of a file in unix

2008-09-09 Thread Rodrick Brown
On Tue, Sep 9, 2008 at 3:23 PM,  <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> i want get the time specified in last line of the file.
> For eg:
> $tail -2 test.log
> 2008 aug 25 14:48:42.800 Sending ping message;
> 2008 aug 25 14:48:43.390 Sending ping message;
> The file size is huge, so i dont want to read the entire file to get
> the last line. Is there any way to get the last line of a file
> directly in perl? i need to get the hour(14) and min(48) specified in
> the last line of the file. can anybody help me on this?
>
> Thanks in advance
>

You can mimic tail -1 using the following:

perl -nle 'push @l,$_;  END { print $l[$#l] }' /tmp/filename


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



-- 
[ Rodrick R. Brown ]
http://www.rodrickbrown.com http://www.linkedin.com/in/rodrickbrown

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




Re: read the time from last line of a file in unix

2008-09-09 Thread Mr. Shawn H. Corey
On Tue, 2008-09-09 at 12:23 -0700, [EMAIL PROTECTED] wrote:
> Hi All,
> 
> i want get the time specified in last line of the file.
> For eg:
> $tail -2 test.log
> 2008 aug 25 14:48:42.800 Sending ping message;
> 2008 aug 25 14:48:43.390 Sending ping message;
> The file size is huge, so i dont want to read the entire file to get
> the last line. Is there any way to get the last line of a file
> directly in perl? i need to get the hour(14) and min(48) specified in
> the last line of the file. can anybody help me on this?
> 
> Thanks in advance
> 
> 

You can use seek to position the file handle near the end of the file.
Make sure to back it up enough from the end of the file to get more than
one line.  Then read lines, preserving the last.

See `perldoc -f seek`


-- 
Just my 0.0002 million dollars worth,
  Shawn

"Where there's duct tape, there's hope."
Cross Time Cafe

"Perl is the duct tape of the Internet."
Hassan Schroeder, Sun's first webmaster


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




Re: read the time from last line of a file in unix

2008-09-09 Thread John W. Krahn

[EMAIL PROTECTED] wrote:

Hi All,


Hello,


i want get the time specified in last line of the file.
For eg:
$tail -2 test.log
2008 aug 25 14:48:42.800 Sending ping message;
2008 aug 25 14:48:43.390 Sending ping message;
The file size is huge, so i dont want to read the entire file to get
the last line. Is there any way to get the last line of a file
directly in perl? i need to get the hour(14) and min(48) specified in
the last line of the file. can anybody help me on this?


Use File::ReadBackwards to get just the last line of the file:

http://search.cpan.org/~uri/File-ReadBackwards-1.04/


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: read the time from last line of a file in unix

2008-09-09 Thread Jeff Pang
2008/9/10  <[EMAIL PROTECTED]>:
> Hi All,
>
> i want get the time specified in last line of the file.
> For eg:
> $tail -2 test.log
> 2008 aug 25 14:48:42.800 Sending ping message;
> 2008 aug 25 14:48:43.390 Sending ping message;
> The file size is huge, so i dont want to read the entire file to get
> the last line. Is there any way to get the last line of a file
> directly in perl?

The unix tail's Perl extension is File::Tail:
http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm

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




read the time from last line of a file in unix

2008-09-09 Thread devika84
Hi All,

i want get the time specified in last line of the file.
For eg:
$tail -2 test.log
2008 aug 25 14:48:42.800 Sending ping message;
2008 aug 25 14:48:43.390 Sending ping message;
The file size is huge, so i dont want to read the entire file to get
the last line. Is there any way to get the last line of a file
directly in perl? i need to get the hour(14) and min(48) specified in
the last line of the file. can anybody help me on this?

Thanks in advance


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




Re: how to read the last line of a file directly?

2008-07-07 Thread Randal L. Schwartz
> "Thomas" == Thomas Bätzler <[EMAIL PROTECTED]> writes:

Thomas> Off the top of my head:

And off the top of your head, you reinvented File::ReadBackwards
for no real purpose.

Might as well use the tested module instead.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

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




RE: how to read the last line of a file directly?

2008-07-06 Thread Thomas Bätzler
loody <[EMAIL PROTECTED]> asked:
> I try to read the last line of a file directly instead of using
> while(<>) or something else to read each line until "undef" 
> bumped to me.
> If you know some build-in functions or another modules for me 
> to use, please help me.

Off the top of my head:

#!/usr/bin/perl -w

use strict;
use Fcntl qw(SEEK_END);

my $file = 'somefile.txt';

open( IN, '<', $file ) or die "Can't open '$file': $!";

my $size = -s $file;
my $found;
my $offset = 256;

while( ! defined( $found ) ){
  # prevent seek from overshooting the start of the file.
  $offset = $size if $offset > $size;
  seek( IN, -$offset, SEEK_END ) or warn "seek failed: $!";

  # read line(s) from filehandle. Ideally, offset was chosen
  # such that @lines contains part of the penultimate line
  # and the last line.
  my @lines = ;

  
  if( @lines > 1 ){
# more than one lines means we can be sure we do have the last one.
$found = $lines[ -1 ];
  } elsif( $offset >= $size ) {
# if we have read the whole file and it's just one line, then that is the 
last line.
    $found = $lines[ 0 ];
  } else {
# try again with a bigger offset
$offset += 256;
  }
}

print "The last line is: $found\n";
__END__

HTH,
Thomas

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




Re: how to read the last line of a file directly?

2008-07-06 Thread Amit Saxena
Hi

Though I am not very sure, but can we use inbuilt seek function in perl ?

Regards,
Amit Saxena

On Mon, Jul 7, 2008 at 12:39 AM, Rob Dixon <[EMAIL PROTECTED]> wrote:

> loody wrote:
> >
> > I try to read the last line of a file directly instead of using
> > while(<>) or something else to read each line until "undef" bumped to
> > me.
> > If you know some build-in functions or another modules for me to use,
> > please help me.
>
> Like this. Both Fcntl and Tie::File are standard modules.
>
> HTH,
>
> Rob
>
>
>
> use strict;
> use warnings;
>
> use Fcntl;
> use Tie::File;
>
> tie my @file, 'Tie::File', 'filename', mode => O_RDONLY or die $!;
> print $file[-1];
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>


Re: how to read the last line of a file directly?

2008-07-06 Thread Rob Dixon
loody wrote:
> 
> I try to read the last line of a file directly instead of using
> while(<>) or something else to read each line until "undef" bumped to
> me.
> If you know some build-in functions or another modules for me to use,
> please help me.

Like this. Both Fcntl and Tie::File are standard modules.

HTH,

Rob



use strict;
use warnings;

use Fcntl;
use Tie::File;

tie my @file, 'Tie::File', 'filename', mode => O_RDONLY or die $!;
print $file[-1];

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




Re: how to read the last line of a file directly?

2008-07-06 Thread Gunnar Hjalmarsson

loody wrote:

I look at http://perldoc.perl.org/index-modules-F.html but I cannot
see File::Tail.


File::Tail is not a core module. Neither is File::ReadBackwards which 
was suggested by somebody else.


You get non-core modules from CPAN http://search.cpan.org/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: how to read the last line of a file directly?

2008-07-06 Thread Telemachus Odysseos
On Sun Jul 06 2008 @  8:09, loody wrote:
> Dear all:
> I try to read the last line of a file directly instead of using
> while(<>) or something else to read each line until "undef" bumped to
> me.
> If you know some build-in functions or another modules for me to use,
> please help me.

My first thought is File::ReadBackwards from the CPAN. Then it should be as
simple as this:
  
#!/usr/bin/perl
use strict;
use warnings;
use File::ReadBackwards;

my $bw = File::ReadBackwards->new( $ARGV[0] )
or die "Can't read $ARGV[0]: $!";

my $last_line = $bw->readline();

print "$last_line";

That takes the file to read from the command line, but you could obviously
specify what file (or files) you want to read in other ways.

Hope this helps, T

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




Re: how to read the last line of a file directly?

2008-07-06 Thread loody
Hi:
I look at http://perldoc.perl.org/index-modules-F.html but I cannot
see File::Tail.
Would you please tell me where I can get the document describing how to use it?
appreciate your help,
miloody

2008/7/6 Aruna Goke <[EMAIL PROTECTED]>:
> loody wrote:
>>
>> Dear all:
>> I try to read the last line of a file directly instead of using
>> while(<>) or something else to read each line until "undef" bumped to
>> me.
>> If you know some build-in functions or another modules for me to use,
>> please help me.
>>
>> appreciate your help,
>> miloody
>>
> use File::Tail module
>
> goksie
>

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




Re: how to read the last line of a file directly?

2008-07-06 Thread Aruna Goke

loody wrote:

Dear all:
I try to read the last line of a file directly instead of using
while(<>) or something else to read each line until "undef" bumped to
me.
If you know some build-in functions or another modules for me to use,
please help me.

appreciate your help,
miloody


use File::Tail module

goksie

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




how to read the last line of a file directly?

2008-07-06 Thread loody
Dear all:
I try to read the last line of a file directly instead of using
while(<>) or something else to read each line until "undef" bumped to
me.
If you know some build-in functions or another modules for me to use,
please help me.

appreciate your help,
miloody

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




Re: Last line issue

2008-01-27 Thread Dr.Ruud
"John W. Krahn" schreef:

>  tr/\t/ /s;

To also squash adjacent space characters:

  tr/\t / /s;

-- 
Affijn, Ruud

"Gewoon is een tijger."

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




Re: Last line issue

2008-01-26 Thread John W. Krahn

Andrej Kastrin wrote:


John W. Krahn wrote:


This should do what you want:

#!/usr/bin/perl
use warnings;
use strict;

my $FNI = shift;
my $FNO = "$FNI.dat";

open my $OUT, '>', $FNO or die "Cannot open '$FNO' $!";
open my $IN,  '<', $FNI or die "Cannot open '$FNI' $!";

my ( $id, $line );
while ( <$IN> ) {
if ( m!! .. m!! ) {
( $id, $line ) = ( $1, '' ) if m!(\d+)!;
s/\A\s+//;
s/\s+\z//;
tr/\t/ /s;   # more efficient than s/\t+/ /g
$line .= $_ if /Id|To|From/;
print $OUT "$id\t$line\n" if m!/Note!;
}
}

close $IN;
close $OUT;


many, many thanks for your quick answer.

I modified your script a bit:

$line .= $_ if /Id|To|From/;
print $OUT "$id\t$line\n" if m!/Note!;


to:
$line .= $_ if m!! .. m!!;
print $OUT "$id\t$line\n" if m!!;


but some problem still persists with the output:
001 
001ThomasJoanafoo
002 
002JohnPaulafoo
003 
003AndrewMariafoo


Note that there is no opening  tag at the beginning.


This should work better:

my ( $id, $line );
while ( <$IN> ) {
if ( m!! .. m!! ) {
s/\A\s+//;
s/\s+\z//;
tr/\t/ /s;
$id = $1 if m!(\d+)!;
if ( m!! ) {
$line = $_;
}
else {
$line .= $_;
}
print $OUT "$id\t$line\n" if m!/Note!;
}
}



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: Last line issue

2008-01-26 Thread Andrej Kastrin

Dear Jonh,

many, many thanks for your quick answer.

I modified your script a bit:

$line .= $_ if /Id|To|From/;
print $OUT "$id\t$line\n" if m!/Note!;


to:
$line .= $_ if m!! .. m!!;
print $OUT "$id\t$line\n" if m!!;


but some problem still persists with the output:
001 
001ThomasJoanafoo

002 002JohnPaulafoo
003 
003AndrewMariafoo


Note that there is no opening  tag at the beginning.

Best, Andrej






John W. Krahn wrote:

Andrej Kastrin wrote:

Dear all,


Hello,

to pre-process my XML dataset in run simple Perl script on it, which 
extract Id identifier from XML data and paste the whole XML record to 
it. For example, the input data looks like:




001
Thomas
Joana


002
John
Paula


003
Andrew
Maria



and the desire output using the script should be:

001001ThomasJoana
002002JohnPaula
003003AndrewMaria


This should do what you want:

#!/usr/bin/perl
use warnings;
use strict;

my $FNI = shift;
my $FNO = "$FNI.dat";

open my $OUT, '>', $FNO or die "Cannot open '$FNO' $!";
open my $IN,  '<', $FNI or die "Cannot open '$FNI' $!";

my ( $id, $line );
while ( <$IN> ) {
if ( m!! .. m!! ) {
( $id, $line ) = ( $1, '' ) if m!(\d+)!;
s/\A\s+//;
s/\s+\z//;
tr/\t/ /s;   # more efficient than s/\t+/ /g
$line .= $_ if /Id|To|From/;
print $OUT "$id\t$line\n" if m!/Note!;
}
}

close $IN;
close $OUT;



But I can't figure why the script below omit the last record in the 
input dataset, e.g.:


Your second while loop is eating up the third record without outputting 
anything.




John


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




Re: Last line issue

2008-01-26 Thread John W. Krahn

Andrej Kastrin wrote:

Dear all,


Hello,

to pre-process my XML dataset in run simple Perl script on it, which 
extract Id identifier from XML data and paste the whole XML record to 
it. For example, the input data looks like:




001
Thomas
Joana


002
John
Paula


003
Andrew
Maria



and the desire output using the script should be:

001001ThomasJoana
002002JohnPaula
003003AndrewMaria


This should do what you want:

#!/usr/bin/perl
use warnings;
use strict;

my $FNI = shift;
my $FNO = "$FNI.dat";

open my $OUT, '>', $FNO or die "Cannot open '$FNO' $!";
open my $IN,  '<', $FNI or die "Cannot open '$FNI' $!";

my ( $id, $line );
while ( <$IN> ) {
if ( m!! .. m!! ) {
( $id, $line ) = ( $1, '' ) if m!(\d+)!;
s/\A\s+//;
s/\s+\z//;
tr/\t/ /s;   # more efficient than s/\t+/ /g
$line .= $_ if /Id|To|From/;
print $OUT "$id\t$line\n" if m!/Note!;
}
}

close $IN;
close $OUT;



But I can't figure why the script below omit the last record in the 
input dataset, e.g.:


Your second while loop is eating up the third record without outputting 
anything.




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Last line issue

2008-01-26 Thread Andrej Kastrin

Dear all,

to pre-process my XML dataset in run simple Perl script on it, which 
extract Id identifier from XML data and paste the whole XML record to 
it. For example, the input data looks like:




001
Thomas
Joana


002
John
Paula


003
Andrew
Maria



and the desire output using the script should be:

001 001ThomasJoana
002 002JohnPaula
003 003AndrewMaria

But I can't figure why the script below omit the last record in the 
input dataset, e.g.:


001 001ThomasJoana
002 002JohnPaula

I'd appreciate any suggestions or pointers.
Best, Andrej


## test.pl ##
use strict;
my $FNI = shift;
my $FNO = "$FNI.dat";
my $started = 0;
my $chunk;
my @chunk;

open OUT, ">$FNO";
open IN, "$FNI";
while () {
s/^\s+//g;
s/\s+$//g;
if (m/\/) {
if ($started) {
my $clob = join("", @chunk);
&process_chunk($clob);
} else {
$started = 1;
}
@chunk = ();
push (@chunk, $_);
while (1) {
$chunk = ;
$chunk =~ s/^\s+//g;
$chunk =~ s/\s+$//g;
push (@chunk, $chunk);
last if ($chunk =~ m/\<\/Note>/);
}
}
}
close IN;
close OUT;

sub process_chunk {
my $clob = shift;
$clob =~ s/\t+/ /g;
my $id;
if ($clob =~ m/\(\d+)\<\/Id>/) {
$id = $1;
}
print OUT "$id\t$clob\n";
}


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




Re: Get the byte position of previous line from last line in file

2007-11-06 Thread Gunnar Hjalmarsson

sivasakthi wrote:

I have the text file  as following,

this first line
this is the second line
this the third line
this is the fourth line
this is the sixth line
this is the seventh line

while opening and reading that text file, is it possible to get the byte
position of "this is the sixth line" ??


Jeff answered your question.

Assuming that your goal is to insert the missing fifth line, you may 
want to use this approach:


use Tie::File;
tie my @file, 'Tie::File', 'myfile' or die $!;
splice(@file, 4, 0, 'this is the fifth line');
untie @file;

perldoc Tie::File
perldoc -f splice

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: Get the byte position of previous line from last line in file

2007-11-05 Thread Jeff Pang


-Original Message-
>From: sivasakthi <[EMAIL PROTECTED]>

>
>
>while opening and reading that text file, is it possible to get the byte
>position of "this is the sixth line" ??
>

Yes. see perldoc -f tell

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




Get the byte position of previous line from last line in file

2007-11-05 Thread sivasakthi
Hi All,

I have the text file  as following,

this first line
this is the second line
this the third line
this is the fourth line
this is the sixth line
this is the seventh line


while opening and reading that text file, is it possible to get the byte
position of "this is the sixth line" ??



Thanks,
Siva




RE: CUt first and last line from a file (DATA)

2003-09-10 Thread Paul Kraus
Not tested

While (){
my @record = split /\|/;
next unless ($record[6]);  # go to next line if all 7 fields are
not populated
.
}


Unless I am misunderstanding what you are trying to do that should work.

I don't get why you would want to skip the first and second lines
though. 
However if they are not records the code above will have them skipped.

-Original Message-
From: LoneWolf [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, September 10, 2003 5:04 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: CUt first and last line from a file (DATA)


GARNOR|09/10/03
GARNOR|141023|BUDDY|Y54321|[EMAIL PROTECTED]|Y|Y
GARNOR|141033|BUDD|Y5432|[EMAIL PROTECTED]|Y|Y
GARNOR|141043|BUD|Y5432|[EMAIL PROTECTED]|Y|Y
GARNOR|141053|BU|Y54|[EMAIL PROTECTED]|Y|Y


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



CUt first and last line from a file (DATA)

2003-09-10 Thread LoneWolf
GARNOR|09/10/03 
GARNOR|141023|BUDDY|Y54321|[EMAIL PROTECTED]|Y|Y 
GARNOR|141033|BUDD|Y5432|[EMAIL PROTECTED]|Y|Y
GARNOR|141043|BUD|Y5432|[EMAIL PROTECTED]|Y|Y
GARNOR|141053|BU|Y54|[EMAIL PROTECTED]|Y|Y


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: CUt first and last line from a file

2003-09-10 Thread Paul Kraus
Can you send a sample of the data that you are parsing?

-Original Message-
From: LoneWolf [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, September 10, 2003 4:42 PM
To: [EMAIL PROTECTED]
Subject: CUt first and last line from a file


I'm parsing every file in a directory, cutting whitespace out (per a
previous request) and so far it is working great, but I have come across
a couple of things: 1.  I either need to cut the first and last line of
each file or 2.  I need to skip the line if one of the fields is empty.

I have tried to code 2, however the lines are not working right.

if ($comp_name ne " "){}

leaves in the blank lines.  I have tried to use just single quotes, but
same results.  so the question is, how can I do both 1 and 2.  Some of
the pipe files I am playing with are going to only be 1 or 2 lines, and
parsing them out instead of just cutting the first and last lines and
loading them as-is into the database will be faster then trying to grep
for an empty, but grepping on the empty for other (much larger files)
would be best, so I need to be able to do both.

TIA!

Robert


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



CUt first and last line from a file

2003-09-10 Thread LoneWolf
I'm parsing every file in a directory, cutting whitespace out (per a
previous request) and so far it is working great, but I have come across a
couple of things:
1.  I either need to cut the first and last line of each file
or
2.  I need to skip the line if one of the fields is empty.

I have tried to code 2, however the lines are not working right.

if ($comp_name ne " "){}

leaves in the blank lines.  I have tried to use just single quotes, but same
results.  so the question is, how can I do both 1 and 2.  Some of the pipe
files I am playing with are going to only be 1 or 2 lines, and parsing them
out instead of just cutting the first and last lines and loading them as-is
into the database will be faster then trying to grep for an empty, but
grepping on the empty for other (much larger files) would be best, so I need
to be able to do both.

TIA!

Robert


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to get 1st line, last line and no of lines in a file

2003-02-20 Thread Lance
I told you my 'nix was rusty... ;-)

"Gary Stainburn" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Hi folks,
>
>
> On Wednesday 19 Feb 2003 9:15 am, Lance wrote:
> > On a Unix system you could use 'lc' to count the lines and 'top' or
'tail'
> > to read the first or last lines.  My Unix is getting rusty, but there
are
> > functions to do what you want - so you could do something like:
> >
> > my $linecount = `cat file.txt| lc`;
>
> Quite often I see commands like this, and I have to wonder why?
>
> The above command forks a process and runs 'cat'.  It then forks another
> process 'lc' and pipes the output from cat to the input of lc.  This seems
a
> lot of overhead when you can simply supply lc with the file name to read.
>
> BTW, lc doesn't exist on my box, so I'd have to use 'wc -l' which I'm
guessing
> 'lc' was just an alias to anyway.   If you use
>
> my $linecount=`wc -l file.txt`;
>
> you'll give your machine less work to do.  Also, I don't know how well DOS
> systems handle pipes and multiple processes.  In fact you probably don't
have
> 'wc' on a DOS system either.  You'll probably find that your routine is as
> good as it gets anyway.
>
> Gary
>
> >
> > to get the line count. I'm sure that the lc command needs something
else,
> > so you will have to play with it to get it to work.  I used to use
> > something like this in ksh to do line counts on lines with millions of
> > lines, and it would return pretty quick - but that was on some pretty
> > impressive hardware...
> >
> > dunno what to do in the DOS world, other than the 'expensive' file
> > processing.
> >
> > PS, I really need to get back into 'nix.  I can't believe I have
forgotten
> > such *simple* stuff... ugh.
> >
> >
> > "Toby Stuart" <[EMAIL PROTECTED]> wrote in message
> > [EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> >
> > > > -Original Message-
> > > > From: Madhu Reddy [mailto:[EMAIL PROTECTED]]
> > > > Sent: Wednesday, February 19, 2003 1:25 PM
> > > > To: [EMAIL PROTECTED]
> > > > Subject: How to get 1st line, last line and no of lines in a file
> > > >
> > > >
> > > > Hi,
> > > >How to get first line, last line and no of lines in
> > > > a file.
> > > >
> > > > is there any perl functions available for that ?
> > > > right now what i am doing is
> > > >
> > > > open file
> > > > while (
> > > > {
> > > >  $lines++;
> > > > }
> > > > close(FH)
> > > >
> > > > This operation is expensive..
> > > > suppose, if file have millions of records,
> > > > it will take more time
> > > >
> > > > I think there should be some functions to get those..
> > > > i appreciate u r help
> > > >
> > > > Thanx in advance
> > > > -Madhu
> > >
> > > perldoc -q "number of lines in a file"
> > >
> > > Found in E:\Perl\lib\pod\perlfaq5.pod
> > >   How do I count the number of lines in a file?
> > >
> > > One fairly efficient way is to count newlines in the file.
> > > The following program uses a feature of tr///, as documented in
> >
> > the
> >
> > > perlop manpage. If your text file doesn't end with a
newline,
> > > then it's not really a proper text file, so this may
report
> >
> > one
> >
> > > fewer line than you expect.
> > >
> > > $lines = 0;
> > > open(FILE, $filename) or die "Can't open `$filename':
> > > $!"; while (sysread FILE, $buffer, 4096) {
> > > $lines += ($buffer =~ tr/\n//);
> > > }
> > > close FILE;
> > >
> > > This assumes no funny games with newline translations.
>
> --
> Gary Stainburn
>
> This email does not contain private or confidential material as it
> may be snooped on by interested government parties for unknown
> and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: How to get 1st line, last line and no of lines in a file

2003-02-20 Thread Gary Stainburn
Hi folks,


On Wednesday 19 Feb 2003 9:15 am, Lance wrote:
> On a Unix system you could use 'lc' to count the lines and 'top' or 'tail'
> to read the first or last lines.  My Unix is getting rusty, but there are
> functions to do what you want - so you could do something like:
>
> my $linecount = `cat file.txt| lc`;

Quite often I see commands like this, and I have to wonder why?

The above command forks a process and runs 'cat'.  It then forks another 
process 'lc' and pipes the output from cat to the input of lc.  This seems a 
lot of overhead when you can simply supply lc with the file name to read.

BTW, lc doesn't exist on my box, so I'd have to use 'wc -l' which I'm guessing 
'lc' was just an alias to anyway.   If you use

my $linecount=`wc -l file.txt`;

you'll give your machine less work to do.  Also, I don't know how well DOS 
systems handle pipes and multiple processes.  In fact you probably don't have 
'wc' on a DOS system either.  You'll probably find that your routine is as 
good as it gets anyway. 

Gary

>
> to get the line count. I'm sure that the lc command needs something else,
> so you will have to play with it to get it to work.  I used to use
> something like this in ksh to do line counts on lines with millions of
> lines, and it would return pretty quick - but that was on some pretty
> impressive hardware...
>
> dunno what to do in the DOS world, other than the 'expensive' file
> processing.
>
> PS, I really need to get back into 'nix.  I can't believe I have forgotten
> such *simple* stuff... ugh.
>
>
> "Toby Stuart" <[EMAIL PROTECTED]> wrote in message
> [EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
>
> > > -Original Message-
> > > From: Madhu Reddy [mailto:[EMAIL PROTECTED]]
> > > Sent: Wednesday, February 19, 2003 1:25 PM
> > > To: [EMAIL PROTECTED]
> > > Subject: How to get 1st line, last line and no of lines in a file
> > >
> > >
> > > Hi,
> > >How to get first line, last line and no of lines in
> > > a file.
> > >
> > > is there any perl functions available for that ?
> > > right now what i am doing is
> > >
> > > open file
> > > while (
> > > {
> > >  $lines++;
> > > }
> > > close(FH)
> > >
> > > This operation is expensive..
> > > suppose, if file have millions of records,
> > > it will take more time
> > >
> > > I think there should be some functions to get those..
> > > i appreciate u r help
> > >
> > > Thanx in advance
> > > -Madhu
> >
> > perldoc -q "number of lines in a file"
> >
> > Found in E:\Perl\lib\pod\perlfaq5.pod
> >   How do I count the number of lines in a file?
> >
> > One fairly efficient way is to count newlines in the file.
> > The following program uses a feature of tr///, as documented in
>
> the
>
> > perlop manpage. If your text file doesn't end with a newline,
> > then it's not really a proper text file, so this may report
>
> one
>
> > fewer line than you expect.
> >
> > $lines = 0;
> > open(FILE, $filename) or die "Can't open `$filename':
> > $!"; while (sysread FILE, $buffer, 4096) {
> > $lines += ($buffer =~ tr/\n//);
> > }
> > close FILE;
> >
> > This assumes no funny games with newline translations.

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: How to get 1st line, last line and no of lines in a file

2003-02-20 Thread Lance
On a Unix system you could use 'lc' to count the lines and 'top' or 'tail'
to read the first or last lines.  My Unix is getting rusty, but there are
functions to do what you want - so you could do something like:

my $linecount = `cat file.txt| lc`;

to get the line count. I'm sure that the lc command needs something else, so
you will have to play with it to get it to work.  I used to use something
like this in ksh to do line counts on lines with millions of lines, and it
would return pretty quick - but that was on some pretty impressive
hardware...

dunno what to do in the DOS world, other than the 'expensive' file
processing.

PS, I really need to get back into 'nix.  I can't believe I have forgotten
such *simple* stuff... ugh.


"Toby Stuart" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
>
>
> > -Original Message-
> > From: Madhu Reddy [mailto:[EMAIL PROTECTED]]
> > Sent: Wednesday, February 19, 2003 1:25 PM
> > To: [EMAIL PROTECTED]
> > Subject: How to get 1st line, last line and no of lines in a file
> >
> >
> > Hi,
> >How to get first line, last line and no of lines in
> > a file.
> >
> > is there any perl functions available for that ?
> > right now what i am doing is
> >
> > open file
> > while (
> > {
> >  $lines++;
> > }
> > close(FH)
> >
> > This operation is expensive..
> > suppose, if file have millions of records,
> > it will take more time
> >
> > I think there should be some functions to get those..
> > i appreciate u r help
> >
> > Thanx in advance
> > -Madhu
> >
>
>
> perldoc -q "number of lines in a file"
>
> Found in E:\Perl\lib\pod\perlfaq5.pod
>   How do I count the number of lines in a file?
>
> One fairly efficient way is to count newlines in the file. The
> following program uses a feature of tr///, as documented in
the
> perlop manpage. If your text file doesn't end with a newline,
> then it's not really a proper text file, so this may report
one
> fewer line than you expect.
>
> $lines = 0;
> open(FILE, $filename) or die "Can't open `$filename': $!";
> while (sysread FILE, $buffer, 4096) {
> $lines += ($buffer =~ tr/\n//);
> }
> close FILE;
>
> This assumes no funny games with newline translations.
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: How to get 1st line, last line and no of lines in a file

2003-02-19 Thread Paul
> > Subject: How to get 1st line, last line and no of lines in a file
> > is there any perl functions available for that ?
> > suppose, if file have millions of records,

ok
If it's a small file, try Tie::File by (I believe) Mark Jason Dominus.
It's very cool.

For a big file, I'm not sure there's a better way than to just run
through and count lines as you goand I don't just mean for Perl.
Lines are delimited by newline characters, and can be of varying
lengths, so how else would you could them? You could do something like
reading the whole file in by blocks and using m// or tr/// to count the
newlines, but that's still pretty intensive.

__
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: How to get 1st line, last line and no of lines in a file

2003-02-19 Thread R. Joseph Newton
Madhu Reddy wrote:

> Hi,
>How to get first line, last line and no of lines in
> a file.
>
> is there any perl functions available for that ?
> right now what i am doing is
>
> open file
> while (
> {
>  $lines++;
> }
> close(FH)
>
> This operation is expensive..
> suppose, if file have millions of records,
> it will take more time
>
> I think there should be some functions to get those..
> i appreciate u r help
>
> Thanx in advance
> -Madhu

It depends on how much control you have over the syorage system.  This is really a 
design issue, when you get to mangement of iles of that size.  If I were using such a 
massive file, I would try to access it only through some management mechanism that 
kept track of basic overall information about the larger file.  Could be a simple set 
of name-value pairs, or perhaps asectioned file description.

Then you would manage any additions, de3letions or edits to the file so that the 
associated metadata file is kept updated.  Depending on the way the file is organized, 
it might also be useful to store inidces to bookmarks within the file, so that you can 
use seek methods to locate specific areas.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: How to get 1st line, last line and no of lines in a file

2003-02-18 Thread Toby Stuart


> -Original Message-
> From: Madhu Reddy [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, February 19, 2003 1:25 PM
> To: [EMAIL PROTECTED]
> Subject: How to get 1st line, last line and no of lines in a file
> 
> 
> Hi,
>How to get first line, last line and no of lines in
> a file.
> 
> is there any perl functions available for that ?
> right now what i am doing is
> 
> open file
> while (
> {
>  $lines++;
> }
> close(FH)
> 
> This operation is expensive..
> suppose, if file have millions of records,
> it will take more time
> 
> I think there should be some functions to get those..
> i appreciate u r help
> 
> Thanx in advance
> -Madhu
> 


perldoc -q "number of lines in a file"

Found in E:\Perl\lib\pod\perlfaq5.pod
  How do I count the number of lines in a file?

One fairly efficient way is to count newlines in the file. The
following program uses a feature of tr///, as documented in the
perlop manpage. If your text file doesn't end with a newline,
then it's not really a proper text file, so this may report one
fewer line than you expect.

$lines = 0;
open(FILE, $filename) or die "Can't open `$filename': $!";
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close FILE;

This assumes no funny games with newline translations.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




How to get 1st line, last line and no of lines in a file

2003-02-18 Thread Madhu Reddy
Hi,
   How to get first line, last line and no of lines in
a file.

is there any perl functions available for that ?
right now what i am doing is

open file
while (
{
 $lines++;
}
close(FH)

This operation is expensive..
suppose, if file have millions of records,
it will take more time

I think there should be some functions to get those..
i appreciate u r help

Thanx in advance
-Madhu





__
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Last Line in Packages

2002-12-11 Thread Bob Showalter
> -Original Message-
> From: NYIMI Jose (BMB) [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, December 11, 2002 1:00 PM
> To: popeye _; [EMAIL PROTECTED]
> Subject: RE: Last Line in Packages
> 
> 
> See this archive :
> 
> http://archive.develooper.com/beginners%40perl.org/msg38109.html
> 
> José.

That archive gives an incorrect explanation.

Better to study

   perldoc -f require

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Last Line in Packages

2002-12-11 Thread NYIMI Jose (BMB)
See this archive :

http://archive.develooper.com/beginners%40perl.org/msg38109.html

José.

> -Original Message-
> From: popeye _ [mailto:[EMAIL PROTECTED]] 
> Sent: Monday, December 09, 2002 11:47 PM
> To: [EMAIL PROTECTED]
> Subject: Last Line in Packages
> 
> 
> Often, but not always, I see the last line in a perl module as
> 
>1;
> 
> Is this to force the module to exit 'true', forcing it to compile?
> 
> 
> Thanks!
> 
> 
> 
> Jeff
> 
> 
> 
> 
> 
> _
> Tired of spam? Get advanced junk mail protection with MSN 8. 
> http://join.msn.com/?page=features/junkmail
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


 DISCLAIMER 

"This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Last Line in Packages

2002-12-11 Thread popeye _
Often, but not always, I see the last line in a perl module as

  1;

Is this to force the module to exit 'true', forcing it to compile?


Thanks!



Jeff





_
Tired of spam? Get advanced junk mail protection with MSN 8. 
http://join.msn.com/?page=features/junkmail


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Grab last line like `tail'

2002-12-01 Thread Danijel Tasov
Harry Putnam wrote:
> Is there a perl equivalent to the unix `tail' command?  Where I could
> grab the last line from a file without having to read the whole file?

There is the module File::ReadBackwards;

# perl -MCPAN -e 'install File::ReadBackwards'
$ perldoc File::ReadBackwards;

bye,
Da.Ta



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Grab last line like `tail'

2002-12-01 Thread Harry Putnam
Is there a perl equivalent to the unix `tail' command?  Where I could
grab the last line from a file without having to read the whole file?



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: oneliner to delete last line of file

2002-11-29 Thread John W. Krahn
Ramprasad A Padmanabhan wrote:
> 
> Hello all

Hello,

> I am using redhat linux 7.2
> 
> I am required to delete the last line of a file. Now I am doing a
> cumbersome thing like this
> 
> perl -e '@_=<>;pop @_;print @_;' $FILE > $FILE.tmp
> mv $FILE.tmpl $FILE
> 
> Cant I do it any better

Here is one way to do it.  :-)

perl -MTie::File -e'tie @file,"Tie::File",shift;pop @file' $FILE



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: oneliner to delete last line of file

2002-11-29 Thread Sudarshan Raghavan
On Fri, 29 Nov 2002, Ramprasad A Padmanabhan wrote:

> 
> Hello all
> 
> I am using redhat linux 7.2
> 
> I am required to delete the last line of a file. Now I am doing a
> cumbersome thing like this
> 
> perl -e '@_=<>;pop @_;print @_;' $FILE > $FILE.tmp
> mv $FILE.tmpl $FILE
> 
> Cant I do it any better

perl -i.tmp -pe 'eof and undef $_' $FILE

perldoc -f eof
perldoc perlrun



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




oneliner to delete last line of file

2002-11-29 Thread Ramprasad A Padmanabhan

Hello all

I am using redhat linux 7.2

I am required to delete the last line of a file. Now I am doing a
cumbersome thing like this

perl -e '@_=<>;pop @_;print @_;' $FILE > $FILE.tmp
mv $FILE.tmpl $FILE

Cant I do it any better


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: how to get the last line of a file

2002-10-11 Thread david

Alex Chen wrote:

> hi, all
> 
> i want to know how to get the last line of
> a file .i know the func read has a paramenter offset but i don't know how
> to use it.please help!!!
> 
> thanks
>   alex chen

try this:

#!/usr/bin/perl -w;
use strict;

my $line='';
my $byte='';

open(FH,"foo.txt") || die $!;
seek(FH,(-s "foo.txt")-2,0);

while(1){

read(FH,$byte,1);

print $line and last if($byte eq "\n");
$line = $byte . $line;

seek(FH,tell(FH)-2,0);
}
close(FH);

__END__

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: how to get the last line of a file

2002-10-11 Thread John W. Krahn

Alex Chen wrote:
> 
> hi, all

Hello,

> i want to know how to get the last line of
> a file .i know the func read has a paramenter offset but i don't know how to
> use it.please help!!!


1)  Install http://search.cpan.org/author/URI/File-ReadBackwards-0.98/

use File::ReadBackwards;
my $bw = File::ReadBackwards->new( 'file' ) or die "Cannot read 'file'
$!";
my $last_line = $bw->readline;


2)  Use the Tie::File module

use Tie::File;
tie my @lines, 'Tie::File', 'file' or die "Cannot read 'file' $!";
my $last_line = $lines[-1];


3)  Read through the whole file

open FILE, 'file' or die "Cannot read 'file' $!";
my $last_line;
$last_line = $_ while ;


4)  Read the file like File::ReadBackwards does

use Fcntl ':seek';

my $size = 1024;
my $buffer = '';
my $last_line;

sysopen FILE, 'file', O_RDONLY or die "Cannot open 'file' $!";
binmode FILE;

my $pos = sysseek FILE, 0, SEEK_END or die "Cannot seek on 'file' $!";
while ( $pos ) {
$pos = 0 if ($pos -= $size) < 0;
sysseek FILE, $pos, SEEK_SET or die "Cannot seek on 'file' $!";
sysread FILE, my $temp, $size or die "Cannot read 'file' $!";
chomp( $buffer = $temp . $buffer );
if ( ( $index = rindex $buffer, $/ ) >= 0 ) {
$last_line = substr $buffer, $index + length $/;
last;
}
}




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: how to get the last line of a file

2002-10-11 Thread NYIMI Jose (BMB)

use Tie::File;
tie @array, 'Tie::File', "file.txt" or die $!;
my $last_line=$array[$#array];

José.


> -Original Message-
> From: alex chen [mailto:[EMAIL PROTECTED]] 
> Sent: Friday, October 11, 2002 8:57 AM
> To: [EMAIL PROTECTED]
> Subject: how to get the last line of a file
> 
> 
> hi, all
> 
> i want to know how to get the last line of
> a file .i know the func read has a paramenter offset but i 
> don't know how to use it.please help!!!
> 
> thanks
>   alex chen
> 
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


 DISCLAIMER 

"This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: how to get the last line of a file

2002-10-11 Thread David Garamond

alex chen wrote:
> hi, all
> 
> i want to know how to get the last line of
> a file .i know the func read has a paramenter offset but i don't know how to
> use it.please help!!!

the easy way (but inefficient):

  # read until the last line
  open F, "file.txt" or die $!;
  $last=$_ while ;

the more (though probably not the *most*) efficient way:

  # position pointer at the end of file, and then search for newlines
  open F, "file.txt" or die $!;
  seek F, 0, 2;
  while ($pos = tell F) {
seek F, -($pos > 1024 ? 1024 : $pos), 1 or die;
read F, $block, 1024;
$_ .= $block;
do {$last=$1; print ">>$last"; last} if /.+\n(.+)/s;
  }
  print "Last line is: $last";

hope it helps.

-- 
dave


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: how to get the last line of a file

2002-10-11 Thread Jean Padilla

Hi, Alex

i suggest the following:

#!/usr/local/bin/perl -w
my $filename = "Your_file_name";

# if you are so lucky to work on Unix
my $lastline = `tail -1 $filename`;
print $lastline;

# if file is small enough to hold in an array
open(FILE, $filename) or die "Can't open $filename.\n";
my @array = ();
close(FILE);
$lastline = $array[-1];
print $lastline;

# otherwise, read file line by line and retain the last one
open(FILE, $filename) or die "Can't open $filename.\n";
while($_ = ) {$lastline = $_;};
close(FILE);
print $lastline;

Hope it helps.



alex chen a écrit :
> 
> hi, all
> 
> i want to know how to get the last line of
> a file .i know the func read has a paramenter offset but i don't know how to
> use it.please help!!!
> 
> thanks
>   alex chen
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


how to get the last line of a file

2002-10-11 Thread alex chen

hi, all

i want to know how to get the last line of
a file .i know the func read has a paramenter offset but i don't know how to
use it.please help!!!

thanks
  alex chen



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Last line of file...

2001-12-18 Thread Michael R. Wolf

"James Kelty" <[EMAIL PROTECTED]> writes:

> Well, I thought of that earlier, but I also thought that I
> was not guaranteed that the order of an array was
> unreliable, so I may not actually be getting the 'last' of
> the file. True or untrue ?

By definition:
Arrays are ordered.
Diamond operator is ordered.
Assigning diamond operator to array is ordered.

Rely on it -- it's reliable.
Code to it.

-- 
Michael R. Wolf
All mammals learn by playing!
   [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Last line of file...

2001-12-18 Thread John W. Krahn

James Kelty wrote:
> 
> Is there a document in perldoc that tells the best way to get the last line
> of a file? Below is my usual code for reading a file.
> 
> #!/usr/bin/perl -w
> 
> $file = qq(/some/file/);
> 
> open FILE, "$file" or die "Cannot open file: $!\n";
> 
> while() {
>do something with the line;
> }
> 
> close(FILE);
> 
> What I want to do is read just the last line. Might help in the case of a
> password file or something like that.


use File::ReadBackwards;

my $file = q(/some/file);

$bw = File::ReadBackwards->new( $file ) or die "can't read $file: $!";
$last_line = $bw->readline;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Last line of file...

2001-12-18 Thread Peter Cornelius

If you know the size of the last line you can use 'seek' to get there, but
this operates on bytes, not chars.  If the records are of a fixed size this
would be the most efficient way to do it.

use POSIX; #This gives us the SEEK_END constant
seek (FH, -$recsize, SEEK_END) or die "Could not seek: $!";
$last = ;

But if the records have variable lengths you end up seeking to the end, read
a char, seek back 2, read 1, seek back 2... until you get the next record
separator.

If you have really big files this might be worth while, but you will have
more maintainable code if you forget about this type of optimization until
it's shown itself to be a problem.  I'd stick with the idiom I posted
earlier if the files are guaranteed to be small or the loop structure
proposed by smoot if the files may be large.

Peter C.

-Original Message-

If the file is huge I wouldn't recommend doing so.. because it puts all
the file into your array

I think you can go directly to the last line if you know exactly the
length of the last line, am I wrong on this one? Like seeking to the end
and reading backwards of n chars?

Etienne


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Last line of file...

2001-12-18 Thread Peter Cornelius

You are probably thinking of associative arrays.  They are declared with the
'%' character in the lead and do not have any intelligible order.  With a
normal array the order is guaranteed.

If your files aren't to big I've found this idiom useful when I just want
the last line.

open (FH, "smallfile") or die "Couldn't open smallfile";
($last_line) = reverse ;
close (FH);

I believe this reads the entire file into memory so you wouldn't want to do
it with very large files.

Take a look at perldoc -f reverse for more info.

Hope this helps,
Peter C.

-Original Message-

Well, I thought of that earlier, but I also thought that I was not
guaranteed that the order of an array was unreliable, so I may not actually
be getting the 'last' of the file. True or untrue ?

- Really Original Message-


In that case, do this..

open(IN, "filename");
@file=;
print "$file[$#file]\n";

- Really Really Original Message -

> Thank you, but I would like to programmatically do it from perl rather
than
> using shell commands. Make the whole script more portable.
>
> -James
>
>
> -Really Really Original Message-
>
> If this is all you want your script to do, I suggest using this command
>
> tail -n1 filename
>
>
> - Original Message -
> > Is there a document in perldoc that tells the best way to get the last
> line
> > of a file? Below is my usual code for reading a file.
> >
> >
> > #!/usr/bin/perl -w
> >
> > $file = qq(/some/file/);
> >
> > open FILE, "$file" or die "Cannot open file: $!\n";
> >
> > while() {
> >do something with the line;
> > }
> >
> > close(FILE);
> >
> >
> > What I want to do is read just the last line. Might help in the case of
a
> > password file or something like that.
> >
> > Thanks!
> >
> > -James



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Last line of file...

2001-12-18 Thread Michael Fowler

On Tue, Dec 18, 2001 at 10:43:54AM -0800, James Kelty wrote:
> Well, I thought of that earlier, but I also thought that I was not
> guaranteed that the order of an array was unreliable, so I may not actually
> be getting the 'last' of the file. True or untrue ?

Arrays are ordered, they must be, that is their purpose; they provide
ordered access to a sequence of values.  You are probably confusing them
with hashes, which are occasionally called "associative arrays".  The
ordering of a hash should be thought of as random.  It's not, really, but
it's safest to assume as much.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Last line of file...

2001-12-18 Thread Agustin Rivera

You are right.  I would suggest going with Smoot Carl-Mitchell's suggestion.

Agustin Rivera
Webmaster, Pollstar.com
http://www.pollstar.com



- Original Message -
From: "Etienne Marcotte" <[EMAIL PROTECTED]>
To: "Agustin Rivera" <[EMAIL PROTECTED]>
Cc: "James Kelty" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, December 18, 2001 11:02 AM
Subject: Re: Last line of file...


> If the file is huge I wouldn't recommend doing so.. because it puts all
> the file into your array
>
> I think you can go directly to the last line if you know exactly the
> length of the last line, am I wrong on this one? Like seeking to the end
> and reading backwards of n chars?
>
> Etienne
>
> Agustin Rivera wrote:
> >
> > In that case, do this..
> >
> > open(IN, "filename");
> > @file=;
> > print "$file[$#file]\n";
> >
> > Agustin Rivera
> > Webmaster, Pollstar.com
> > http://www.pollstar.com
> >
> > - Original Message -
> > From: "James Kelty" <[EMAIL PROTECTED]>
> > To: "Agustin Rivera" <[EMAIL PROTECTED]>
> > Sent: Tuesday, December 18, 2001 10:27 AM
> > Subject: RE: Last line of file...
> >
> > > Thank you, but I would like to programmatically do it from perl rather
> > than
> > > using shell commands. Make the whole script more portable.
> > >
> > > -James
> > >
> > >
> > > -Original Message-
> > > From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
> > > Sent: Tuesday, December 18, 2001 10:30 AM
> > > To: James Kelty; [EMAIL PROTECTED]
> > > Subject: Re: Last line of file...
> > >
> > >
> > > If this is all you want your script to do, I suggest using this
command
> > >
> > > tail -n1 filename
> > >
> > > Agustin Rivera
> > > Webmaster, Pollstar.com
> > > http://www.pollstar.com
> > >
> > >
> > >
> > > - Original Message -
> > > From: "James Kelty" <[EMAIL PROTECTED]>
> > > To: <[EMAIL PROTECTED]>
> > > Sent: Tuesday, December 18, 2001 10:21 AM
> > > Subject: Last line of file...
> > >
> > >
> > > > Is there a document in perldoc that tells the best way to get the
last
> > > line
> > > > of a file? Below is my usual code for reading a file.
> > > >
> > > >
> > > > #!/usr/bin/perl -w
> > > >
> > > > $file = qq(/some/file/);
> > > >
> > > > open FILE, "$file" or die "Cannot open file: $!\n";
> > > >
> > > > while() {
> > > >do something with the line;
> > > > }
> > > >
> > > > close(FILE);
> > > >
> > > >
> > > > What I want to do is read just the last line. Might help in the case
of
> > a
> > > > password file or something like that.
> > > >
> > > > Thanks!
> > > >
> > > > -James
> > > >
> > > >
> > > >
> > > >
> > > > --
> > > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
>
> --
> Etienne Marcotte
> Specifications Management - Quality Control
> Imperial Tobacco Ltd. - Montreal (Qc) Canada
> 514.932.6161 x.4001
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Last line of file...

2001-12-18 Thread Etienne Marcotte

If the file is huge I wouldn't recommend doing so.. because it puts all
the file into your array

I think you can go directly to the last line if you know exactly the
length of the last line, am I wrong on this one? Like seeking to the end
and reading backwards of n chars?

Etienne

Agustin Rivera wrote:
> 
> In that case, do this..
> 
> open(IN, "filename");
> @file=;
> print "$file[$#file]\n";
> 
> Agustin Rivera
> Webmaster, Pollstar.com
> http://www.pollstar.com
> 
> - Original Message -
> From: "James Kelty" <[EMAIL PROTECTED]>
> To: "Agustin Rivera" <[EMAIL PROTECTED]>
> Sent: Tuesday, December 18, 2001 10:27 AM
> Subject: RE: Last line of file...
> 
> > Thank you, but I would like to programmatically do it from perl rather
> than
> > using shell commands. Make the whole script more portable.
> >
> > -James
> >
> >
> > -Original Message-
> > From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
> > Sent: Tuesday, December 18, 2001 10:30 AM
> > To: James Kelty; [EMAIL PROTECTED]
> > Subject: Re: Last line of file...
> >
> >
> > If this is all you want your script to do, I suggest using this command
> >
> > tail -n1 filename
> >
> > Agustin Rivera
> > Webmaster, Pollstar.com
> > http://www.pollstar.com
> >
> >
> >
> > - Original Message -
> > From: "James Kelty" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Tuesday, December 18, 2001 10:21 AM
> > Subject: Last line of file...
> >
> >
> > > Is there a document in perldoc that tells the best way to get the last
> > line
> > > of a file? Below is my usual code for reading a file.
> > >
> > >
> > > #!/usr/bin/perl -w
> > >
> > > $file = qq(/some/file/);
> > >
> > > open FILE, "$file" or die "Cannot open file: $!\n";
> > >
> > > while() {
> > >do something with the line;
> > > }
> > >
> > > close(FILE);
> > >
> > >
> > > What I want to do is read just the last line. Might help in the case of
> a
> > > password file or something like that.
> > >
> > > Thanks!
> > >
> > > -James
> > >
> > >
> > >
> > >
> > > --
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

-- 
Etienne Marcotte
Specifications Management - Quality Control
Imperial Tobacco Ltd. - Montreal (Qc) Canada
514.932.6161 x.4001

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Last line of file...

2001-12-18 Thread James Kelty

Ok, thanks!

-James

-Original Message-
From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 18, 2001 10:49 AM
To: James Kelty; [EMAIL PROTECTED]
Subject: Re: Last line of file...


I've never had an instance where that didn't work.  I use for $a(0..$#array)
loops in almost all of my scripts.

Agustin Rivera
Webmaster, Pollstar.com
http://www.pollstar.com



- Original Message -
From: "James Kelty" <[EMAIL PROTECTED]>
To: "Agustin Rivera" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, December 18, 2001 10:43 AM
Subject: RE: Last line of file...


> Well, I thought of that earlier, but I also thought that I was not
> guaranteed that the order of an array was unreliable, so I may not
actually
> be getting the 'last' of the file. True or untrue ?
>
> -James
>
>
> -Original Message-
> From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, December 18, 2001 10:44 AM
> To: James Kelty; [EMAIL PROTECTED]
> Subject: Re: Last line of file...
>
>
> In that case, do this..
>
> open(IN, "filename");
> @file=;
> print "$file[$#file]\n";
>
> Agustin Rivera
> Webmaster, Pollstar.com
> http://www.pollstar.com
>
>
>
> - Original Message -
> From: "James Kelty" <[EMAIL PROTECTED]>
> To: "Agustin Rivera" <[EMAIL PROTECTED]>
> Sent: Tuesday, December 18, 2001 10:27 AM
> Subject: RE: Last line of file...
>
>
> > Thank you, but I would like to programmatically do it from perl rather
> than
> > using shell commands. Make the whole script more portable.
> >
> > -James
> >
> >
> > -Original Message-
> > From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
> > Sent: Tuesday, December 18, 2001 10:30 AM
> > To: James Kelty; [EMAIL PROTECTED]
> > Subject: Re: Last line of file...
> >
> >
> > If this is all you want your script to do, I suggest using this command
> >
> > tail -n1 filename
> >
> > Agustin Rivera
> > Webmaster, Pollstar.com
> > http://www.pollstar.com
> >
> >
> >
> > - Original Message -
> > From: "James Kelty" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Tuesday, December 18, 2001 10:21 AM
> > Subject: Last line of file...
> >
> >
> > > Is there a document in perldoc that tells the best way to get the last
> > line
> > > of a file? Below is my usual code for reading a file.
> > >
> > >
> > > #!/usr/bin/perl -w
> > >
> > > $file = qq(/some/file/);
> > >
> > > open FILE, "$file" or die "Cannot open file: $!\n";
> > >
> > > while() {
> > >do something with the line;
> > > }
> > >
> > > close(FILE);
> > >
> > >
> > > What I want to do is read just the last line. Might help in the case
of
> a
> > > password file or something like that.
> > >
> > > Thanks!
> > >
> > > -James
> > >
> > >
> > >
> > >
> > > --
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Last line of file...

2001-12-18 Thread smoot

> "McCollum, Frank" <[EMAIL PROTECTED]> said:

> I'm a beginner too, given, but alternatively, you could unshift the lines
> into an array and just check the last variable.  This would allow you to
> reference the other lines later if there was more work to be done here.

Something like this while loop fragment is a simple way to approach the problem:

my $last_line = "";
while () {
$last_line = $_;
}

$last_line will then hold the last line of the file with the trailing
line terminator.  You can chomp it off, if it is unneeded.

-- 
Smoot Carl-Mitchell
Consultant



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Last line of file...

2001-12-18 Thread Agustin Rivera

I've never had an instance where that didn't work.  I use for $a(0..$#array)
loops in almost all of my scripts.

Agustin Rivera
Webmaster, Pollstar.com
http://www.pollstar.com



- Original Message -
From: "James Kelty" <[EMAIL PROTECTED]>
To: "Agustin Rivera" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, December 18, 2001 10:43 AM
Subject: RE: Last line of file...


> Well, I thought of that earlier, but I also thought that I was not
> guaranteed that the order of an array was unreliable, so I may not
actually
> be getting the 'last' of the file. True or untrue ?
>
> -James
>
>
> -Original Message-
> From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, December 18, 2001 10:44 AM
> To: James Kelty; [EMAIL PROTECTED]
> Subject: Re: Last line of file...
>
>
> In that case, do this..
>
> open(IN, "filename");
> @file=;
> print "$file[$#file]\n";
>
> Agustin Rivera
> Webmaster, Pollstar.com
> http://www.pollstar.com
>
>
>
> - Original Message -
> From: "James Kelty" <[EMAIL PROTECTED]>
> To: "Agustin Rivera" <[EMAIL PROTECTED]>
> Sent: Tuesday, December 18, 2001 10:27 AM
> Subject: RE: Last line of file...
>
>
> > Thank you, but I would like to programmatically do it from perl rather
> than
> > using shell commands. Make the whole script more portable.
> >
> > -James
> >
> >
> > -Original Message-
> > From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
> > Sent: Tuesday, December 18, 2001 10:30 AM
> > To: James Kelty; [EMAIL PROTECTED]
> > Subject: Re: Last line of file...
> >
> >
> > If this is all you want your script to do, I suggest using this command
> >
> > tail -n1 filename
> >
> > Agustin Rivera
> > Webmaster, Pollstar.com
> > http://www.pollstar.com
> >
> >
> >
> > - Original Message -
> > From: "James Kelty" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Tuesday, December 18, 2001 10:21 AM
> > Subject: Last line of file...
> >
> >
> > > Is there a document in perldoc that tells the best way to get the last
> > line
> > > of a file? Below is my usual code for reading a file.
> > >
> > >
> > > #!/usr/bin/perl -w
> > >
> > > $file = qq(/some/file/);
> > >
> > > open FILE, "$file" or die "Cannot open file: $!\n";
> > >
> > > while() {
> > >do something with the line;
> > > }
> > >
> > > close(FILE);
> > >
> > >
> > > What I want to do is read just the last line. Might help in the case
of
> a
> > > password file or something like that.
> > >
> > > Thanks!
> > >
> > > -James
> > >
> > >
> > >
> > >
> > > --
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Last line of file...

2001-12-18 Thread James Kelty

Well, I thought of that earlier, but I also thought that I was not
guaranteed that the order of an array was unreliable, so I may not actually
be getting the 'last' of the file. True or untrue ?

-James


-Original Message-
From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 18, 2001 10:44 AM
To: James Kelty; [EMAIL PROTECTED]
Subject: Re: Last line of file...


In that case, do this..

open(IN, "filename");
@file=;
print "$file[$#file]\n";

Agustin Rivera
Webmaster, Pollstar.com
http://www.pollstar.com



- Original Message -
From: "James Kelty" <[EMAIL PROTECTED]>
To: "Agustin Rivera" <[EMAIL PROTECTED]>
Sent: Tuesday, December 18, 2001 10:27 AM
Subject: RE: Last line of file...


> Thank you, but I would like to programmatically do it from perl rather
than
> using shell commands. Make the whole script more portable.
>
> -James
>
>
> -Original Message-
> From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, December 18, 2001 10:30 AM
> To: James Kelty; [EMAIL PROTECTED]
> Subject: Re: Last line of file...
>
>
> If this is all you want your script to do, I suggest using this command
>
> tail -n1 filename
>
> Agustin Rivera
> Webmaster, Pollstar.com
> http://www.pollstar.com
>
>
>
> - Original Message -
> From: "James Kelty" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Tuesday, December 18, 2001 10:21 AM
> Subject: Last line of file...
>
>
> > Is there a document in perldoc that tells the best way to get the last
> line
> > of a file? Below is my usual code for reading a file.
> >
> >
> > #!/usr/bin/perl -w
> >
> > $file = qq(/some/file/);
> >
> > open FILE, "$file" or die "Cannot open file: $!\n";
> >
> > while() {
> >do something with the line;
> > }
> >
> > close(FILE);
> >
> >
> > What I want to do is read just the last line. Might help in the case of
a
> > password file or something like that.
> >
> > Thanks!
> >
> > -James
> >
> >
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Last line of file...

2001-12-18 Thread Agustin Rivera

In that case, do this..

open(IN, "filename");
@file=;
print "$file[$#file]\n";

Agustin Rivera
Webmaster, Pollstar.com
http://www.pollstar.com



- Original Message -
From: "James Kelty" <[EMAIL PROTECTED]>
To: "Agustin Rivera" <[EMAIL PROTECTED]>
Sent: Tuesday, December 18, 2001 10:27 AM
Subject: RE: Last line of file...


> Thank you, but I would like to programmatically do it from perl rather
than
> using shell commands. Make the whole script more portable.
>
> -James
>
>
> -Original Message-
> From: Agustin Rivera [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, December 18, 2001 10:30 AM
> To: James Kelty; [EMAIL PROTECTED]
> Subject: Re: Last line of file...
>
>
> If this is all you want your script to do, I suggest using this command
>
> tail -n1 filename
>
> Agustin Rivera
> Webmaster, Pollstar.com
> http://www.pollstar.com
>
>
>
> - Original Message -
> From: "James Kelty" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Tuesday, December 18, 2001 10:21 AM
> Subject: Last line of file...
>
>
> > Is there a document in perldoc that tells the best way to get the last
> line
> > of a file? Below is my usual code for reading a file.
> >
> >
> > #!/usr/bin/perl -w
> >
> > $file = qq(/some/file/);
> >
> > open FILE, "$file" or die "Cannot open file: $!\n";
> >
> > while() {
> >do something with the line;
> > }
> >
> > close(FILE);
> >
> >
> > What I want to do is read just the last line. Might help in the case of
a
> > password file or something like that.
> >
> > Thanks!
> >
> > -James
> >
> >
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Last line of file...

2001-12-18 Thread McCollum, Frank

I'm a beginner too, given, but alternatively, you could unshift the lines
into an array and just check the last variable.  This would allow you to
reference the other lines later if there was more work to be done here.

-Original Message-
From: James Kelty [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 18, 2001 1:22 PM
To: [EMAIL PROTECTED]
Subject: Last line of file...


Is there a document in perldoc that tells the best way to get the last line
of a file? Below is my usual code for reading a file.


#!/usr/bin/perl -w

$file = qq(/some/file/);

open FILE, "$file" or die "Cannot open file: $!\n";

while() {
   do something with the line;
}

close(FILE);


What I want to do is read just the last line. Might help in the case of a
password file or something like that.

Thanks!

-James




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Last line of file...

2001-12-18 Thread Agustin Rivera

If this is all you want your script to do, I suggest using this command

tail -n1 filename

Agustin Rivera
Webmaster, Pollstar.com
http://www.pollstar.com



- Original Message -
From: "James Kelty" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, December 18, 2001 10:21 AM
Subject: Last line of file...


> Is there a document in perldoc that tells the best way to get the last
line
> of a file? Below is my usual code for reading a file.
>
>
> #!/usr/bin/perl -w
>
> $file = qq(/some/file/);
>
> open FILE, "$file" or die "Cannot open file: $!\n";
>
> while() {
>do something with the line;
> }
>
> close(FILE);
>
>
> What I want to do is read just the last line. Might help in the case of a
> password file or something like that.
>
> Thanks!
>
> -James
>
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Last line of file...

2001-12-18 Thread James Kelty

Is there a document in perldoc that tells the best way to get the last line
of a file? Below is my usual code for reading a file.


#!/usr/bin/perl -w

$file = qq(/some/file/);

open FILE, "$file" or die "Cannot open file: $!\n";

while() {
   do something with the line;
}

close(FILE);


What I want to do is read just the last line. Might help in the case of a
password file or something like that.

Thanks!

-James




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Last line

2001-09-07 Thread Bob Showalter

> -Original Message-
> From: Jorge Goncalvez [mailto:[EMAIL PROTECTED]]
> Sent: Friday, September 07, 2001 6:10 AM
> To: [EMAIL PROTECTED]
> Subject: Re:Last line
> 
> 
> Hi, how to parse the last line of a file.
> 
> I have this code:
> $pb->destroy unless($last_line=~/^dhcpd|LOG_INFO|ftpd/);
> 
> $last_line=??
> 
> The file is already open.

No magic way to read the last line. You can read forward
through the file line-by-line, saving the last-read line. When you
get to eof, you'll have the last line saved. Simple, but not good 
if the file is huge.

Otherwise, you would typically seek to some offset before the
file length and read a block of data. Then scan backwards to find
the line separator to extract the last line.

CPAN has a File::Tail module which does all this and more. That's
what I would use.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: last line

2001-07-27 Thread Walt Mankowski

COLLINEAU writes:
> How can i do to delete the last line of a file ?

>From perlfaq5...

 How do I change one line in a file/delete a line in a
 file/insert a line in the middle of a file/append to the
 beginning of a file?

 

 In the unique case of deleting lines at the end of a file,
 you can use tell() and truncate().  The following code
 snippet deletes the last line of a file without making a
 copy or reading the whole file into memory:

 open (FH, "+< $file");
 while (  ) { $addr = tell(FH) unless eof(FH) }
 truncate(FH, $addr);

 Error checking is left as an exercise for the reader.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: last line

2001-07-27 Thread Greg Meckes

--- COLLINEAU Franck FTRD/DMI/TAM <[EMAIL PROTECTED]> wrote:
> Hi!
> 
> How can i do to delete the last line of a file ?
> 
> Thanks
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

Try:
#Get the data
open(FILE, $file) or die "Couldn't open $file: $! \n";
my @FileData = ;
close(FILE);

#How many lines:
my $HowMany = @FileData;

#Make sure you have something

if($HowMany > 0) {
#remove last line
pop@FileData;

#write back to the file
open(FILE, ">$file") or die "Couldn't open $file: $! \n";
print @FileData;
close(FILE);

}


Of course you can do more checking and verification 

Greg


__
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




last line

2001-07-27 Thread COLLINEAU Franck FTRD/DMI/TAM

Hi!

How can i do to delete the last line of a file ?

Thanks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]