Operating System - HP-UX
1753859 Members
7282 Online
108809 Solutions
New Discussion юеВ

yet another quick perl question

 
SOLVED
Go to solution
Steven E. Protter
Exalted Contributor

yet another quick perl question

Got a perl script:

my $refer = "\"certified.shmtl\"";
print "Before: ",$refer,"\n";
# $refer =~ tr ?/??d;
print "After : ",$refer,"\n";

The first print statement prints

"certified.shtml"

I need to strip the trailing and leading double quotes out of the string. Can't seem to make line 3 (commented) do that for me.

Thanks to A. Clay Stephenson forthe base code.

Background: while moving a html document from one location to another, I'm pulling alont the VIRTUAL include document embedded in the document as well.

The line starts out looking like:


I used awk to cook it down to "certified.shtml"

Good Solution: 1 line of perl that strips out the trailing and leading double quotes.

Pefect solution: Perl code that goes straight through the html docu and gives me certified.shtml

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
10 REPLIES 10
Ken Penland_1
Trusted Contributor
Solution

Re: yet another quick perl question

$refer =~ s/\"//g;
'
H.Merijn Brand (procura
Honored Contributor

Re: yet another quick perl question

my $refer = qq{"certified.shmtl"}; # /me woders if that should have been shtml

print "Before: $refer\n"; # Wy list, just interpolate!
$refer =~ tr/"//d; # or $refer =~ s/"//g;
print "After: $refer\n";

Safe and generic:

$refer =~ s/^\s*"?(.*)"?\s*$//;

And your one-liner:

# perl -nle'm{include\s+virtual="([^"]+)"} and print $1' blah.html

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steven E. Protter
Exalted Contributor

Re: yet another quick perl question

Thats a bunny.

I'll leave the thread open a few hours in case someone wants to do the Perfect solution.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
H.Merijn Brand (procura
Honored Contributor

Re: yet another quick perl question

Drat! I cannot type *that* fast!
Enjoy, Have FUN! H.Merijn
Steven E. Protter
Exalted Contributor

Re: yet another quick perl question

Note:

The perl script was already running, so a one liner was not actually needed.

What was asked in the perfect answer was a few lines of code to search the a different file say named column.shtml for that



and get the filename certified.shtml into a variable.

Thread still open, a chance for Merijn to double his pointage.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
H.Merijn Brand (procura
Honored Contributor

Re: yet another quick perl question

# perl -nle'm{include\s+virtual="([^"]+)"}i and print $1' column.shtml

or inside the script

:
:
m{include\s+virtual="([^"]+)"}i and print $file = $1;
:
:

$file now contains "certified.shtml"

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Hein van den Heuvel
Honored Contributor

Re: yet another quick perl question

If that syntax is stable, then you can exploit the double-quote to split the line on:

Some variation of:

perl -ne 'if (/INCLUDE VIRT/) { $file=(split(/"/))[1]; print "file=$file\n "}' input-data.


Steven E. Protter
Exalted Contributor

Re: yet another quick perl question

Having a testing issue.

#!/opt/perl/bin/perl


# $refer =~ s/\"//g;
$refer = "stevefile";

m{include\s+virtual="([^"]+)"}i and print $file = $refer;

print "After : ",$file,"\n";

Does not produce output.


/opt/perl/bin/perl -nle'm{include\s+virtual="([^"]+)"}i and print $1' stevefile

Output:
hpuxws.shtml

searches the file and produces appropriate output.

Kind of wonderinig what I'm doing wrong. I like Merijn's approach and would like to finish the job. I have tried submiting the file stevefile on the command line but that produces nothing new and does not accomplish objective.

The file name inbedded in the file stevefile is the output I want, double quotes stripped.

SEP
~
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
H.Merijn Brand (procura
Honored Contributor

Re: yet another quick perl question

In this snippet:
--8<---
#!/opt/perl/bin/perl

# $refer =~ s/\"//g;
$refer = "stevefile";

m{include\s+virtual="([^"]+)"}i and print $file = $refer;

print "After : ",$file,"\n";
-->8---
I do not see a loop that puts every line of the file being parsed in $_, something that the -n option in

--8<---
/opt/perl/bin/perl -nle'm{include\s+virtual="([^"]+)"}i and print $1' stevefile
-->8---

does. In your script, you match against $_, which is empty. So if you want the *script* to do the same as the oneliner, change it to something like

--8<---
#!/opt/perl/bin/perl

my $file = "?";
@ARGV = ("stevefile");
while (<>) {
m{include\s+virtual="([^"]+)"}i and $file = $1;
}
print "After : ",$file,"\n";
-->8---

and see if that fits your needs

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn