[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3739
  • Last Modified:

perl, head tail remove

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
Richoo
Asked:
Richoo
  • 3
  • 2
1 Solution
 
SkytzoCommented:
I understand the code.  Could you give an example statement that you would like to parse though?
0
 
RichooAuthor Commented:
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
 
SkytzoCommented:

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RichooAuthor Commented:
thank you muchly, I'll try it out later tonight and get back to you
0
 
cluedonCommented:

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
 
SkytzoCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now