Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What the "fastest, most efficient way" is might be a different question. This is a typical way to do something like that:</p> <pre><code>use strict; use warnings; local $/ = ","; my @file = &lt;DATA&gt;; say "Number of records: " . @file; my $half = int((@file/2)+0.5); say "Last half of records ($half):"; say @file[$half .. $#file]; __DATA__ 20121130092403000Server1::RedHat 1.2.3.4(1234),20121130092503000Server2::RedHat 5.6.7.8(1234),20121130092603000Server3::SUSE 9.8.7.6(9876),20121130092703000Server4::WindowsXP 5.6.7.8(6543), </code></pre> <p>Note that the DATA file handle is used for demonstration here. You can simply change <code>&lt;DATA&gt;</code> to <code>&lt;&gt;</code> to make it use file arguments instead.</p> <p>This will be somewhat less efficient because the file is read into memory, and that will consume memory, which can be costly for large files. An alternative is to simply run through the file and count the records, then reopen the file to do the printing. E.g.:</p> <pre><code>my $file = shift; local $/ = ","; open my $fh, "&lt;", $file or die $!; my $count; while (&lt;$fh&gt;) { $count++ } $count = int(($count/2)+0.5); open $fh, "&lt;", $file or die $!; while ($count-- &gt; 0) { &lt;$fh&gt; }; while (&lt;$fh&gt;) { print } </code></pre> <p>And of course these outputs need to be redirected, e.g. like so:</p> <pre><code>perl script.pl oldfile &gt; newfile </code></pre> <p>You may also like the <a href="http://search.cpan.org/perldoc?Tie%3a%3aFile" rel="nofollow"><code>Tie::File</code></a> module. E.g.:</p> <pre><code>use strict; use warnings; use Tie::File; my $file = shift; tie @array, 'Tie::File', $file or die $!; my $half = int((@array/2)+0.5); splice @array, 0, $half; untie @array; </code></pre> <p>Note that the effects of this is irreversible, so make backups before you try it. It is supposedly efficient even for large files, and does not read the file into memory.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload