Operating System - Linux
1753394 Members
7056 Online
108792 Solutions
New Discussion юеВ

Re: Extracting data in between strings...using perl

 
SOLVED
Go to solution
jmckinzie
Super Advisor

Extracting data in between strings...using perl

I have a variable called WebJump in a perl script.

When i print the content it gives me this:


T
his page is used to hold your data while you are being authorized for your reque
st.

You will be forwarded to continue the authorization process. If this
does not happen automatically, please click the Continue button below.
="AUTOSUBMIT" METHOD="POST" STARThttps://test.ip.com/siteminderagent/forms/
login.fcc?TYPE=33554433&REALMOID=06-00034bb7-e037-116f-8241-808d67a50008&GUID=&S
MAUTHREASON=0&METHOD=POST&SMAGENTNAME=$SM$8OJVwItP%2fV8GXRhL%2fhch6KJt3EvC2AWLQ7
%2bWLfTgx3%2bWD7k%2buJc3dVSFPOr1jTxg&TARGET=$SM$%2fEND="HIDDEN" NAME="SMPostPres
erve" VALUE="S1NJbjNmby81VzRqMmo0cTNuWm9NdFo3cVpZSlF6enpMc2laNWZrcnRudlhWVEUzM0x
UTHVPR1Y3REpwNnUwM1ZVd1IySFdQZkRDRmpUQldrV01ybk9pcEFBZnpzNmg4RG1yQ0lRQUNzbTFMekd
iUG9Eck02M2NUcis4RG5YQ3l2dkZHOGp4WDRPbHJJTFdJOXUvbnFBPT0END="SUBMIT" VALUE="Cont
inue">


I want to print everthing between START and END and then reassign it to my WebJump variable.

Any ideas?
11 REPLIES 11
Tim Nelson
Honored Contributor

Re: Extracting data in between strings...using perl

This one is actually an example in the book.

Use sed.

WebJump=`echo "the_stuff"|sed /^BEGIN/,/^END/p`
jmckinzie
Super Advisor

Re: Extracting data in between strings...using perl

How can i do it in PERL?
Tim Nelson
Honored Contributor

Re: Extracting data in between strings...using perl

Alright, almost easy, cept the START is really STARThttp.... and also the END...

Will let you know if I find something. SOrry for the quick wrong answer.

jmckinzie
Super Advisor

Re: Extracting data in between strings...using perl

I can change the START END to anything in need to.

IE

...whatever.....

I simply used search and replace....
Geoff Wild
Honored Contributor

Re: Extracting data in between strings...using perl

Wait for Hein or Merijn to post their fabulous perl answers :)
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Murat SULUHAN
Honored Contributor

Re: Extracting data in between strings...using perl

Hi

If whole text is single line (otherwise please parse into single line)

use index function to find START
use substr function to extract from START to end of the line (str1)

Then you have 2 END in text,
so if you want to extract START and first END
again use index function with str1 to find first END's position. and use substr

if you want to extract BEGIN and second END
use rindex with str1 to find position of second END, then subtract it from len($str1)
use again substr

I have no perl interpreter on this computer so I hope this algorithm will help to you

Best Regards
Murat
Murat Suluhan
James R. Ferguson
Acclaimed Contributor

Re: Extracting data in between strings...using perl

Hi Jody:

I assume from the content that you truly want everything between the strings START and END, disregarding any newline boundries.

Using a copy-and-paste of your data :

# echo ${WebJump}|perl -000 -nle 'print $1 if m/.*START(.*)END.*/s'

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Extracting data in between strings...using perl

Hi (again) Jody:

Actually, here is a better representation of what you probably want. It destructively updates the $WebJump scalar variable:

# cat .jody.pl
#!/usr/bin/perl
use strict;
use warnings;

my $WebJump=q[This page is used to hold your da
ta while you are being authorized for your reque st.

You will be forwarded to continue the authorization process. If th
is does not happen automatically, please click the Continue button below.
est.ip.com/siteminderagent/forms/login.fcc?TYPE=33554433&REALMOID=06-00034bb7-e037-116f-8241-808d67a50008&GUID=&S MAUTHREASON=
0&METHOD=POST&SMAGENTNAME=$SM$8OJVwItP%2fV8GXRhL%2fhch6KJt3EvC2AWLQ7 %2bWLfTgx3%2bWD7k%2buJc3dVSFPOr1jTxg&TARGET=$SM$%2fEND="H
IDDEN"NAME="SMPostPreserve"VALUE="S1NJbjNmby81VzRqMmo0cTNuWm9NdFo3cVpZSlF6enpMc2laNWZrcnRudlhWVEUzM0x UTHVPR1Y3REpwNnUwM1ZVd1I
ySFdQZkRDRmpUQldrV01ybk9pcEFBZnpzNmg4RG1yQ0lRQUNzbTFMekdiUG9Eck02M2NUcis4RG5YQ3l2dkZHOGp4WDRPbHJJTFdJOXUvbnFBPT0END="SUBMIT"VA
LUE="Continue">
];

( $WebJump ) = ( $WebJump =~ /START(.*)END/s );
print "$WebJump\n";

Regards!

...JRF...

Hein van den Heuvel
Honored Contributor

Re: Extracting data in between strings...using perl

[Thanks Geoff! I haven't had much time to troll this forum.]

Jody,

Murat pointed out that there are two ENDs in the example. I believe you indicate that was just a bad example, thus nothing to worry about. (allthough it worries me when folks post bad examples, but that's my problem).

Still, if you did have to deal with nested START/ENDs then you may want to be sure to know about the 'greedyness' of the * in perl.

For example if the data was:

x START y START a b d END z END

JRF writes...
( $WebJump ) = ( $WebJump =~ /START(.*)END/s );

This is fine. But note that the .* in there is greedy and in my example will return " y START a b d END z "

If you wanted the inner START/END, then use: .*?

It will return " a b d "

Cheers!
Hein.