Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to get the 3rd argument

         

iamvela

7:03 pm on Jul 11, 2008 (gmt 0)

10+ Year Member



I have a file of several hundred lines with text. A few sample lines are

1871:1 0.00% http://example.org/filepath1
1872:1 0.00% http://example.org/filepath2
1873:1 0.00% http://example.org/filepath3
1877:1 0.00% http://example.org/filepath4
...etc

How can I extract the 3rd argument (the URL)

Thx!

[edited by: tedster at 6:55 am (utc) on July 12, 2008]
[edit reason] remove specific urls [/edit]

rocknbil

9:17 pm on Jul 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard iamvela, see TOS [webmasterworld.com] for the edit that is to come. :-)

Something like this should do it:


#!/usr/bin/perl
# open the file
open (FILE,"yourfile.txt") or die("Cannot open file for reading $!");
# chomp newline, read line by line
while (chomp($line = <FILE>)) {
# split line on spaces
($id,$percent,$url) = split(/\s/,$line);
# You could do a number of things here, let's do two:
# print the variables and store URL's in an array (hash)
print "ID: $id PERCENT: $percent URL: $url\n";
$urls{$id} = $url;
}
#close file
close(FILE);
#print out contents of list
foreach $v (sort keys %urls) {
print "key: $v value: $urls{$v}\n";
}

perl_diver

12:27 am on Jul 12, 2008 (gmt 0)

10+ Year Member



if the first two fields are always fixed-length:

2020:1 0.00%

You can use unpack() to extract the URLs, it is way more efficient than using split or other regexp. See the pack/unpack functions for details, or the pack/unpack tutorial that comes with perl.

If they are not fixed-length, use split() as shown by rocknbil.

iamvela

12:37 am on Jul 12, 2008 (gmt 0)

10+ Year Member



Thanks, I got a simple solution that I am posting for others' & posterity:

% cat <file-name> ¦awk '{print $3}'

perl_diver

5:09 am on Jul 12, 2008 (gmt 0)

10+ Year Member



and its not even perl.