Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > This must be done with perl isn't it?

Reply
Thread Tools

This must be done with perl isn't it?

 
 
Andries
Guest
Posts: n/a
 
      04-19-2004
Hello there,

I hope someone can help me.
This is my problem:
I have a list of thousands and thousands of the next lines:
----------------------------------------------------------------------
<a href="hs80.htm#halveringstijd"target="topic">halve ringstijd</a><br>
<a href="hs80.htm#hartkleppen" target="topic"></a><br>
<a href="hs80.htm#hartvolume" target="topic"></a><br>
<a href="hs80.htm#hemoglobine" target="topic"></a><br>
<a href="hs80.htm#heteroseksueel " target="topic"></a><br>
<a href="hs80.htm#hijgen" target="topic"></a><br>
<a href="hs80.htm#histamine" target="topic"></a><br>
--------------------------------------------------------------------------------------
I need to copy the word between the # and " and put it after the > and
</a>

It can done by hand like the first line but it can be automated with a
perl script isn't it?

If so I still have a problem can anyone tell me how?


TIA
Andries Meijer

 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      04-20-2004
Andries wrote:
> <a href="hs80.htm#hartkleppen" target="topic"></a><br>
> I need to copy the word between the # and " and put it after the > and
> </a>


Try to track down docs with useful perl one-liners. After studying
a few examples of '-pi.bak -e', you'll see your problem is very easy.

perl -pi.bak -e 's,#(\S+)(" target="topic">),#$1$2$1,' hs*.htm

-Joe
 
Reply With Quote
 
 
 
 
Brandon Allen
Guest
Posts: n/a
 
      04-20-2004
Andries <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> Hello there,
>
> I hope someone can help me.
> This is my problem:
> I have a list of thousands and thousands of the next lines:
> ----------------------------------------------------------------------
> <a href="hs80.htm#halveringstijd"target="topic">halve ringstijd</a><br>
> <a href="hs80.htm#hartkleppen" target="topic"></a><br>
> <a href="hs80.htm#hartvolume" target="topic"></a><br>
> <a href="hs80.htm#hemoglobine" target="topic"></a><br>
> <a href="hs80.htm#heteroseksueel " target="topic"></a><br>
> <a href="hs80.htm#hijgen" target="topic"></a><br>
> <a href="hs80.htm#histamine" target="topic"></a><br>
> --------------------------------------------------------------------------------------
> I need to copy the word between the # and " and put it after the > and
> </a>
>
> It can done by hand like the first line but it can be automated with a
> perl script isn't it?
>
> If so I still have a problem can anyone tell me how?
>
>
> TIA
> Andries Meijer


Here's what I came up with, and I tested it with your samples, so it
seems to work just fine. You have to pass the input and output files
on the command line, so run it like this:

perl input_file.txt output_file.txt

Admittedly, I'm just getting back into PERL after a long break from
it, so if someone knows of a faster/more concise/more efficient way to
accomplish this task, I would be very interested in learning how you
did it.

Here's the code I came up with:

#!/usr/bin/perl -w

open(INPUT, "<$ARGV[0]") or die "Cannot open input file!\n";
open(OUTPUT, ">$ARGV[1]") or die "Cannot open ouput file!\n";
while(my $string_data = <INPUT>)
{
chomp $string_data;
@value = split(/([\#\"])/, $string_data);
printf OUTPUT "<a href=\"hs80.htm\#%s\"
target=\"topic\">%s</a><br>\n",$value[4], $value[4];
}

close(INPUT);
close(OUTPUT);
 
Reply With Quote
 
JDany
Guest
Posts: n/a
 
      04-20-2004
Andries <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> Hello there,
>
> I hope someone can help me.
> This is my problem:
> I have a list of thousands and thousands of the next lines:
> ----------------------------------------------------------------------
> <a href="hs80.htm#halveringstijd"target="topic">halve ringstijd</a><br>
> <a href="hs80.htm#hartkleppen" target="topic"></a><br>
> <a href="hs80.htm#hartvolume" target="topic"></a><br>
> <a href="hs80.htm#hemoglobine" target="topic"></a><br>
> <a href="hs80.htm#heteroseksueel " target="topic"></a><br>
> <a href="hs80.htm#hijgen" target="topic"></a><br>
> <a href="hs80.htm#histamine" target="topic"></a><br>
> --------------------------------------------------------------------------------------
> I need to copy the word between the # and " and put it after the > and
> </a>
>
> It can done by hand like the first line but it can be automated with a
> perl script isn't it?
>
> If so I still have a problem can anyone tell me how?
>
>
> TIA
> Andries Meijer


#!/usr/bin/perl -w
while ( <> ) {
if ( /^([^#]+#)([^"]+)("[^>]+>)(.+)$/ ) {
print "$1$2$3$2$4\n";
}
}
__END__

run it as:
perl fix.pl < infile > outfile
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why must and must not be "final" ? NeoGeoSNK Java 25 11-24-2006 02:02 AM
Someone must have done this Chris Botha ASP .Net 18 09-16-2005 01:06 AM
I asked before and was told it could not be done, well its Done...?? Karen Parker NZ Computing 32 08-27-2004 07:21 AM
I asked before and was told it could not be done, well its Done...?? Karen Parker NZ Computing 7 08-26-2004 02:25 AM
How to let browsers be done when parent is done and not wait for childrn's finishing in CGI Zhidian Du Perl Misc 0 02-21-2004 07:01 PM



Advertisments