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: Beginner:
calling specific lines from a txt file by running the program + the number of the line i want

 



lie soul
Novice

Sep 25, 2012, 4:58 PM

Post #1 of 5 (1127 views)
calling specific lines from a txt file by running the program + the number of the line i want Can't Post

Hello,

I need help, please see what i did and tell me what do i need to do in order to get it right...

I made a txt file looks like the following;

Code
Line 1 
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7


I need to run the program to call any line i want. (perl example.pl 5 file.txt)
if i did 5.... i can see the output from line 1 to line 5 and so on... if i did 2 i can see two lines only...

Here's my code:

Code
#!/usr/bin/perl -w 

$infile = $ARGV[0];
$infile = "n.txt";

#Open file
open IN, "$infile" or die "Cannot read $infile: $!\n";

while ($infile=<IN>)
{
print $infile;
}

close IN;


My code read everything from the text file and i don't know how to run it and tell the program to show me how many lines i want..

Please advice!!


Laurent_R
Veteran / Moderator

Sep 25, 2012, 5:24 PM

Post #2 of 5 (1125 views)
Re: [lie soul] calling specific lines from a txt file by running the program + the number of the line i want [In reply to] Can't Post

Hi, as a start, add the following lines at the beginning of the script:


Code
use strict; 
use warnings;

(The 'use warning;' line is not really mandatory here, since you have the -w flag, whish has essentially the same effect, but modern practice tends to favor the 'use ...' syntax, rather than the -w flag.)

Second, these lines are probably wrong:


Code
$infile = $ARGV[0]; 
$infile = "n.txt";


as the value of $AGV[0] assigned to $infile is lost when you assign another value to the same $infile variable.

Third, you are using $infile to store yet something else in the while loop, the lines of your file, this is bad practice as it is confusing (in fact, I suspect that you don't understand exactly what you are doing here).

I would suggest something like this:


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $line_number = shift; # first argument is the line to be printed
my $infile = shift; # second argument is file name

#Open file
open IN, "$infile" or die "Cannot read $infile: $!\n";

while (my $inline=<IN>) {
print $inline if $inline =~ /$line_number/; # prints the line if the number contained in $line_number is found somewhere in the line
}

close IN;


This is assuming that the line number is found in the line to be printed.

If you just want to print line number x of the file, irrespective of its content, change the condition in the while loop as follows:


Code
     print $inline if $line_number == $.; # $. contains the line number of the current file



There are a couple of other things that could be done better, but at least, this should work.


lie soul
Novice

Sep 25, 2012, 5:47 PM

Post #3 of 5 (1123 views)
Re: [lie soul] calling specific lines from a txt file by running the program + the number of the line i want [In reply to] Can't Post

thanks for your help.. but i want is how can i print amount of lines..

i mean if i type ( perl example.pl 2 file.txt ) i will get the following:
Line 1
Line 2
and will stop.. if i did (perl example.pl 6 file.txt) will print out
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6

>>>> got it?


lie soul
Novice

Sep 25, 2012, 5:51 PM

Post #4 of 5 (1122 views)
Re: [lie soul] calling specific lines from a txt file by running the program + the number of the line i want [In reply to] Can't Post

i just fix it...

i replace it with

print $inline if $line_number >= $.

so i can get any number i want

thanks for your help and note that i'm new to perl and i'm learning by myself

thanks


FishMonger
Veteran / Moderator

Sep 26, 2012, 7:06 AM

Post #5 of 5 (1116 views)
Re: [lie soul] calling specific lines from a txt file by running the program + the number of the line i want [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings; # localize warnings (the -w switch globalizes warnings)

# verify that you recieved 2 args
@ARGV == 2 or die "Usage: $0 <line_number> <file>\n";

my ($line_number, $file) = @ARGV;

# verify that the first arg ($line_number) only contains digits
if ( $line_number =~ /\D/ ) {
die "Usage: $0 <line_number> <file>\n";
}

# open the file (using a lexical var filehandle and the 3 arg form of open)
open my $fh, '<', $file or die "Cannot read '$file': $!\n";

# print all lines upto and including $line_number
while (my $line = <$fh>) {
print $line;
last if $line_number == $.;
}

# close the file
close $fh;


 
 


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

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