CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate: Re: [gaetan3009] sed fetch line: Edit Log

Veteran / Moderator

Nov 1, 2017, 11:42 AM

Views: 4960
Re: [gaetan3009] sed fetch line

No reason to fork another process by going out to the shell to run a sed command. Perl has several ways to get a line on a specific line number.

This is probably how I'd do it.

use strict; 
use warnings;
use autodie;

my $file = 'somefile.txt';
open my $fh, '<', $file;

my $line;
while ($line = <$fh>) { close $fh and last if $. == 4 }

print $line;

(This post was edited by FishMonger on Nov 1, 2017, 11:43 AM)

Edit Log:
Post edited by FishMonger (Veteran) on Nov 1, 2017, 11:43 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives