Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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!

In reply to Re: Question about reading specific line in a text file by Marshall
in thread Question about reading specific line in a text file by palsy017

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-20 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found