Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Found it but extracting it...

Status
Not open for further replies.

cyphrix

Programmer
Nov 16, 2006
27
US
Ok so I wrote a small script that reads a file and extracts the <b>lines</b> that contain the matches I want to extract but I cannot seem to find a way to extract the matches out of those lines. When I run the script I get the following:

1
randomtext randomtext randomtext USER ID: 123456 randomtext

2
randomtext USER ID: 78910 randomtext randomtext randomtext

3
randomtext USER ID: 547695 randomtext USER ID: 345876 random


What I want is just one column of nothing but:

USER ID: 123456
USER ID: 78910
USER ID: 547695
USER ID: 345876

but I don't know how to get rid of everything else or how to tell Perl that all I want is just what has been matched, not the lines in which the match is contained.

This is my code:

Code:
open(INFILE, 'userdata.txt') or die "The file could not be found.";

$count = 0

while(<INFILE>) {
     $TheLine = $_;
     $chomp($TheLine);

     foreach($TheLine =~ /USER ID/) {
          open(OUTFILE, ">>UserIDs.txt");
          print OUTFILE "$_\n";
          print OUTFILE "$TheLine\n\n"; { next }
          $count += 1;
          print OUTFILE "Line Count";
          }
}

print "There were $count matches"

Any help is greatly appreciated. Thanks!
 
Just change the regular expression to save what you're attempting to match explicitly:

Code:
open(INFILE, 'userdata.txt') or die "The file could not be found.";

$count = 0

while(<INFILE>) {
	$TheLine = $_;
	$chomp($TheLine);

	foreach ($TheLine =~ [COLOR=blue]/(USER ID: \d+)/g[/color]) {
		open(OUTFILE, ">>UserIDs.txt");
		print OUTFILE "[COLOR=blue]$1[/color]\n";
		print OUTFILE "$TheLine\n\n"; { next }
		$count += 1;
		print OUTFILE "Line Count";
	}
}

print "There were $count matches"
 
Have a google for perlre, or run perldoc perlre on the commandline of your machine

your code with some mods
Code:
open(INFILE, 'userdata.txt') or die "The file could not be found.";
[COLOR=green]open(OUTFILE, ">>UserIDs.txt");[/color] #no point in closing and opening file for each match
$count = 0[COLOR=red];[/color]

while(<INFILE>) {
     $chomp($_);
     if(/USER ID: [COLOR=blue][b](\d+)[/b][/color]/) {
        print OUTFILE "USER ID: [COLOR=blue][b]$1[/b][/color]\n";
        $count++;
     }
}

print "There were $count matches"
close INFILE;
close OUTFILE;

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Slight syntax error there PaulTEG in $chomp($_); Also, $_ is assumed by default of course.

chomp;

That's a whole 5 extra characters you typed. Be sure to stretch out your fingers [bigcheeks]
 
Thanks guys. I appreciate the help! Will definitely check out CPAN also.

I am having a harder time learning Perl but I prefer it already over Python for the specific work I'm doing.
 
MillerH, cheers for pointing that out.

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top