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:
perl equivalent of sed to select a range in file.

 



csnaruto
Novice

Mar 8, 2012, 5:42 PM

Post #1 of 3 (640 views)
perl equivalent of sed to select a range in file. Can't Post

Hi All,

I'm looking for an equivalent perl code to work like the following sed command.

Quote
sed -n '/begin_pattern/,/end_pattern/p' $myfile


What I currently do in perl is to read the whole file into an array and loop through till I get the range from starting and ending pattern. It works but is very inefficient.

Anyone can help with the equivalent command?

Thanks.


FishMonger
Veteran / Moderator

Mar 8, 2012, 6:05 PM

Post #2 of 3 (637 views)
Re: [csnaruto] perl equivalent of sed to select a range in file. [In reply to] Can't Post

Reading in the file into an array and then loop over the array is almost always the wrong approach. Use a while loop to read it line-by-line and use the range operator to extract the section.

You can read up on the range operator in the perlop perldoc.
i.e., perldoc perlop
http://perldoc.perl.org/perlop.html#Range-Operators


csnaruto
Novice

Mar 8, 2012, 10:43 PM

Post #3 of 3 (629 views)
Re: [FishMonger] perl equivalent of sed to select a range in file. [In reply to] Can't Post

Exactly what I'm looking for. Searching a wrong keyword earlier. Thanks.

 
 


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

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