Hello everyone, hoping someone can point me in the right direction.
I have the following code which finds files that end in .txt then tries to match on a regx. The scripts works fine, the only problem is how can I get the code if no space is found continue reading after the carriage return and the next whitespace? Or is there a better way to do this?
The main issues are:
*That the script prints the entire line not just the URL
*if the URL continues to the next line then it cuts the URL/Link short.
I guess I can check if the TLD's exist in the current line before printing the line and if not continue reading to after the <CR> carriage return until a white space? I guessing that I am making this harder that it can be.
Thanks for the help everyone!
#!/usr/bin/perl
use File::Find;
find(\&url_find, "/tmp/sub_url_test/");
sub url_find() {
if ( -f && \.txt$) { #Find files ending in .txt
open(LOG, "< $File::Find::name") or return(0);
while ( my $LINE = <LOG> ) {
if ( $LINE =~ m/(http:\S*\s)/ ) {
print $LINE;
}
}
}
}
==============
FILE CONTENTS
==============
This is a test this is a test test test http://www.website.
com/getme.html this is test this is a test
This is a test this is a test test test http://www.website2.com/
getme.html this is test this is a test
This is a test this is a test test test http://www.website3.com/getme
.html this is test this is a test
This is a test this is a test test test http://www.website4.com/getme.
+h
tml this is test this is a test
==============
CURRENT OUTPUT
==============
This is a test this is a test test test http://www.website.
This is a test this is a test test test http://www.website2.com/
This is a test this is a test test test http://www.website3.com/getme
This is a test this is a test test test http://www.website4.com/getme.
+h
==============================
OUTPUT THAT I AM HOPING TO SEE
==============================
http://www.website.com/getme.html
http://www.website1.com/getme.html
http://www.website2.com/getme.html
http://www.website3.com/getme.html
http://www.website4.com/getme.html
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.