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:
sed fetch line


New User

Nov 1, 2017, 10:39 AM

Post #1 of 2 (5252 views)
sed fetch line Can't Post

Hi everybody,
I am trying to get a line from the line number of a file.
Do i have to use something like :
my $line = `sed ${number}q $file`
Thank you for your help.

Veteran / Moderator

Nov 1, 2017, 11:42 AM

Post #2 of 2 (5247 views)
Re: [gaetan3009] sed fetch line [In reply to] Can't Post

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)


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

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