Re: perl or bash question ["convert strings in a txt to html links"]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 27 February 2010 17:12, Vadkan Jozsef <jozsi.avadkan@xxxxxxxxx> wrote:
> How can I do that in bash or perl, that I have a txt file, e.g.:
>
> $cat file.txt
> Hi, this is the content of the txt file, that contains links like this:
> http://www.somewhere.it/, and it could contain: http://somewhere.com,
> etc..
> This is the second line, that doesn't contains links..
> ..
> This is the XYZ line, that contains a link: http://www.somewhere.net
> $
>
>
> ...ok.. so how could I make a regexp for this?
>
> Turning:
>
> http://website.org
> http://www.website.org
>
> to this:
>
> <a href=http://website.org>http://website.org</a>
> <a href=http://www.website.org>http://www.website.org</a>
>
> The solution would be:
>
> sed 'SOMEMAGIC' file.txt > file.html
> or
> perl 'SOMEBIGMAGIC' file.txt > file.html

Parsing URIs using regular expressions (as others have suggested) is
harder than it looks. I recommend using a Perl module like URI::Find
(which is available as an RPM for Fedora - yum install perl-URI-Find).

The code looks like this (lightly adapted from the module's documentation):

#!/usr/bin/perl

use strict;
use warnings;

use URI::Find;

sub replace {
  my ($uri, $orig_uri) = @_;

  return qq(<a href="$uri">$orig_uri</a>);
}

my $finder = URI::Find->new(\&replace);

while (<>) {
  $finder->find(\$_);
  print $_;
}

Put that in a file (called, perhaps, urifind) and make that file
executable. You can then run it like this:

./urifind file.txt > file.html

Hope that helps.

Dave...
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux