Forum Moderators: coopster & phranque

Message Too Old, No Replies

Read and strip with regex

Large fill need stripping

         

StanTheMan

3:59 pm on Jul 18, 2003 (gmt 0)

10+ Year Member



I have a large file of test that cann't be imported into a database but I want to strip all the postcode's out of the file, I have tried below but it strips the whole line out. But I want just the variable i.e postcode in a seperate file.

#!/usr/bin/perl
open(MYINPUTFILE, "<input.log");
my(@lines) = <MYINPUTFILE>;
my($line);
foreach $line (@lines) {
$username =~ "/\d{3}/";
print $username;
print "\n";
}
close(MYINPUTFILE);

Can anyone help me?

sugarkane

4:32 pm on Jul 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What format is the post code stored as? ie 5 digits, or combination of letters and digits etc..

And how is each line formatted - comma seperated, space seperated, or...?

StanTheMan

4:48 pm on Jul 18, 2003 (gmt 0)

10+ Year Member



I have the postcode regex sorted but when I output it, it returns the whole line, rather than just the post code variable.

Storyteller

6:59 pm on Jul 18, 2003 (gmt 0)

10+ Year Member



With the syntax you're using for regexp operation, variable $username gets assigned either undef or 1, depending on whether regexp matches. I'm not sure this is what you want, is it?

StanTheMan

12:44 pm on Jul 21, 2003 (gmt 0)

10+ Year Member



No I want to output just the postcode that is being stripped from the code, not the full line.

How would I go about doing this with the code above?

tschild

1:10 pm on Jul 21, 2003 (gmt 0)

10+ Year Member



try


if ($line =~ m/(regexp_matching_a_postcode)/) {print "$1\n"};

or, if one line may contain more than one postcode,


while ($line =~ m/(regexp_matching_a_postcode)/g) {print "$1\n"};

StanTheMan

1:39 pm on Jul 21, 2003 (gmt 0)

10+ Year Member



Many Thanks tschild

Saved again by Webmasterworld. Thanks again.