Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Question about reading specific line in a text file

by palsy017 (Initiate)
on Apr 04, 2009 at 02:03 UTC ( #755403=perlquestion: print w/replies, xml ) Need Help??

palsy017 has asked for the wisdom of the Perl Monks concerning the following question:

Good day. I have a text file with info on line 76 that I would like to copy to a variable and print. But, I need to grab only part of the string that is returned. Here is the code:
open (DATAFILE3, $prog); @lines = <DATAFILE3>; close(DATAFILE3); print "$lines[76]><br>\n";

The string returned is "SECTIONS 1-2-3-4 Offered Every Performance!" but I only need part of that,

"SECTIONS 1-2-3-4" (with the dashes).

I would appreciate your input!

Replies are listed 'Best First'.
Re: Question about reading specific line in a text file
by lakshmananindia (Chaplain) on Apr 04, 2009 at 02:21 UTC

    Use Regular Expression or split function. Read man perlretut for regular expression and perldoc -f split to learn about split

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.

Re: Question about reading specific line in a text file
by nagalenoj (Friar) on Apr 04, 2009 at 02:22 UTC
    Try this..,
    open (DATAFILE3, $prog); @lines = <DATAFILE3>; close(DATAFILE3); $lines[76] =~ s/^(\S+\s+\S+).*/$1/; print "$lines[76]><br>\n"

    Also, it is not a good practice to read the entire file in a single attempt. Think, what happens if the file is huge in size?

    Try something like this to avoid reading the file like the above

    open (DATAFILE3, $prog); while (<DATAFILE3>) { next if 1 .. 75; # flip-flop operator $line = $_; last; } # $line has the 76th line, do your stuff now. close(DATAFILE3);
      Actually, you only need one control statement if you know exactly the line number you want to process, by using $. aka $INPUT_LINE_NUMBER in English.
      while (<DATAFILE3>) { $line = $_, last if $. == 76; }

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Question about reading specific line in a text file
by vinoth.ree (Monsignor) on Apr 04, 2009 at 02:48 UTC

    If you don't know regular expression, simply use split function

    my @var=split / /, $lines[1]; print "$var[0] $var[1]";
Re: Question about reading specific line in a text file
by Marshall (Canon) on Apr 04, 2009 at 23:49 UTC
    Here is some example regex code to get you started on the part about getting what you want from the line.

    Perl Regular expressions have some cool shortcut symbols. Below we have "w" which means a word character (A-Z,a-z_0-9), W means not a word character, s is a whitespace character (\s\t\r\n\f), S is not one.

    The regex means: start at beginning of line, skip any non-word characters if they are there (skips the " ), match a series of word characters followed by one or more spaces, then a field of non-space characters. w doesn't work here because of the "-", so easy was just to say non-whitespace. There are lots of variations on this theme. However you will go far with w,W,s,S and understanding what * and + mean.

    Note that if the match fails, the result is undef.

    #!/usr/bin/perl -w use strict; my $text = '"SECTIONS 1-2-3-4 Offered Every Performance!"'; #one way use regex match... $text =~ m/^\W*(\w+\s+\S+)/; #note doesn't modify $text #just checks for match my $x = $1; print "$x\n"; # another way without using $1 # match is in array context and we use list slice to get # the $1 match data $x = ($text =~ m/^\W*(\w+\s+\S+)/)[0]; print "$x\n"; __END__ prints ..... SECTIONS 1-2-3-4 SECTIONS 1-2-3-4
    Oh, I wouldn't worry about reading whole file at once unless it is truly huge. This is obviously some very specialized file with special meaning to line 76. Use judgment on how big your files are gonna be. If its gonna be huge then the ideas to just read til line 76 are worthwhile. Just saying that often the simple approach is just fine for lots of tasks..added complexity has its own cost: Your time!
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://755403]
Approved by naikonta
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-01-23 09:10 GMT
Find Nodes?
    Voting Booth?