Solved

perl, head tail remove

Posted on 2003-11-20
8
3,731 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 250 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article will show, step by step, how to integrate R code into a R Sweave document
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 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