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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
sed fetch line

 



gaetan3009
New User

Nov 1, 2017, 10:39 AM

Post #1 of 2 (624 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.


FishMonger
Veteran / Moderator

Nov 1, 2017, 11:42 AM

Post #2 of 2 (619 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.

Code
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