?
Solved

perl, head tail remove

Posted on 2003-11-20
8
Medium Priority
?
3,736 Views
Last Modified: 2012-08-14
I'm looking for a solution to take a variable (multiple lines) and remove all text before a certain variable and all points after another variable, I found http://perlmonks.thepen.com/249987.html but I can't seem to figure out the variables / files in it.

Thanks in advance
0
Comment
Question by:Richoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 1

Expert Comment

by:Skytzo
ID: 9795057
I understand the code.  Could you give an example statement that you would like to parse though?
0
 

Author Comment

by:Richoo
ID: 9797431
like <h1>heres a header</h1> for the header

and something like </body> for the tail


also, where is the code for the filename? or is that passed via command line? and eitherway I'd like to be able to pass a variable, which will include all my data to this program

Thanks in advance
0
 
LVL 1

Expert Comment

by:Skytzo
ID: 9798597

Alright.  The code above is way more than what you need.  It takes its input from Standard Input as well.

Here is something that should work with some very minor tweaking.

If the text that you want to strip is in the variable $text, and the code that you want to cut is in $head_cut and the tail to cut is in $tail_cut, then the following should work.

What is does is a substitution.  It first searches for all characters in the string up to and including the pattern that is in $head_cut and removes it.     The /sm at the end of the statement tells it to search over multiple lines.

Similarly, the second part tells it to search for all text starting at $tail_cut and afterwards and remove it.

REMEMBER, that if you are going to use the / character in your patterns, such as in </h1>, then you must escape the charcter in your pattern strings such as <\/h1>.

        $text =~ s/.*$head_cut//sm;
        $text =~ s/$tail_cut.*//sm;

See if that works.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:Richoo
ID: 9800375
thank you muchly, I'll try it out later tonight and get back to you
0
 
LVL 1

Accepted Solution

by:
cluedon earned 500 total points
ID: 10019033

something like this may be faster...

$text =~ /$head_cut(.*)$tail_cut/sm;
$new_value = $1;

some error checking would be better..

if ($text =~ /$head_cut(.*)$tail_cut/sm )
{  $new_value = $1;
}
else
{  print "parse failed \n";
}


0
 
LVL 1

Expert Comment

by:Skytzo
ID: 10347099
I was the first to answer the question, and fully tested the result.  It is solid perl code, and I would like to get the points if this is administratively closed.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question