Solved

perl, head tail remove

Posted on 2003-11-20
8
3,729 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 74
groupSum6 challenge 6 77
Windows Batch File - Display current time and date in FOR command 8 67
Regular Expression Calculator Tester 2 57
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 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…

920 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

17 Experts available now in Live!

Get 1:1 Help Now