Solved

perl, head tail remove

Posted on 2003-11-20
8
3,728 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
Comment Utility
I understand the code.  Could you give an example statement that you would like to parse though?
0
 

Author Comment

by:Richoo
Comment Utility
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
Comment Utility

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Richoo
Comment Utility
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
Comment Utility

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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now