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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help on processing text 1

Status
Not open for further replies.

boty

Programmer
Joined
Jan 7, 2007
Messages
3
Location
RO
Hello ...
2 days ago i started to look for something in perl and it seems i cant find it anywere ...
I have the following text info ... (you can look at it for the pattern after you read the comments below )

_____info.txt______________________________________

number: +01.2345.6789
bday: 22.11.1984
email: user@foo.com
country: US
status: Member
name: John
lastname: Smith
email: Foo@foo.com
descr: member since 2005
car: Touareg
changed: porsche

number: +40.213.957
descr: big boobs
descr: blue eyes
bday: 22.11.1984
descr: hot chick
country: IT
name: lucy
lastname: bitch
email: lucybiatch@foo.com

number: +01.12575.5432
descr: Sick of lucy
name: John
lastname: paul
descr: Hard Worker
descr: looking for .. perl
country: FR
email: john.crazy@foo.com
car: porsche parts from john smith
status: Not registered

_____________________________________________________

What i would like to do ... is to extract the phone number from the group of data that contains name JOHN ...
I have the following patterns ... phone number is allways the first afer a blank line and the line starts with the word NUMBER: ... and the name is somwere in that data field ... but the line allways begins with NAME:

So in the end i would have:
__________________________________
number: +01.2345.6789
name: John

number: +01.12575.5432
name: John
_________________________________

or just the numbers ... becasue my new db will be named john ..

I tryed to build something but really doesnt works out ...
I also tryed TextPipe ... because the database is larger than 600 mb ... so either a perl or textpipe script would help :)
Please i really need help .. after 2 days of searching ..

Thank you very much ..
 
untested code but this is pretty simple so should work:

Code:
my @numbers = ();
open(FH,"yourfile.txt") or die "$!";
MAIN: while (<FH>) {
   if (/^number:\s+(\S+)/) {
      my $p = $1;
      while (<FH>) {
         if (/^name:\s+john/i) {
            push @numbers,$p;  
            next MAIN;
         }
         if (/^\s*$/) {
            next MAIN;
         }
      }
   }
}
close(FH);
print "@numbers";


- Kevin, perl coder unexceptional!
 
Thank you KEVIN that really worked ...
with what i learnded these 3 days and after studyed your solution i was able to change it a little to fit my needs ...
I am mostly into webdesigning .. so i just know some php and things like that .. im VERY VERY NEW at perl ... and i have a project does doesnt give to much time now :( ..

One more question ...

I have a very large database ( over 600 mb )
offcourse that if i try to open it with any program it will fail and block my pc ..

I know there is a way that perl would process my db .. and save the results in a file .. Without blocking pc offcourse ( thats why i tryed to use textpipe but i dont know how to pass the code to textpipe and ill stick to perl as from what ived learned seems easy and looks much like php)

Any hint .. PLS PLS

THANKS AGAIN
 
Managed to copy info into a new file .. but ived seen that perl stores it in memory then writes the file .. im still learning ... so i just prepare to do the real work

i have a project im pretty out of time .. so this learning is fun but does not good .. so i know i sound stupid .. but i would really need help so i wouldnt have to read 100 pages to advance 1 step .. :(

So is there a way of reading the db and in the same time perl would output that into the file .. so it wouldnt consume much memory ?

Thank you :)

BTW .. Thank you again KEVIN .. without your help i would have lost weeks before i could get a glimpse of i should look for or do.
 
boty

When you say 'database', is it just a very large version of the info.txt file you posted above? Or is it a real database (like MySQL, SQL Server 2000, DB2)?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
So is there a way of reading the db and in the same time perl would output that into the file

Code:
open(FH,"yourfile.txt") or die "$!";
open(OUT,">output.txt") or die "$!"
MAIN: while (<FH>) {
   if (/^number:\s+(\S+)/) {
      my $p = $1;
      while (<FH>) {
         if (/^name:\s+john/i) {
            print OUT "$p\n";  
            next MAIN;
         }
         if (/^\s*$/) {
            next MAIN;
         }
      }
   }
}
close(FH);
close(OUT);

but 600MB is 600MB, it's going to take a little while to complete. I don't know why it would block access to your PC though.

- Kevin, perl coder unexceptional!
 
Code:
$infile = "Input.txt";
$outfile = "Output.txt";

open ( IN, "< $infile" ) or die "$!";
   @lines = <IN>;
close ( IN );

open ( OUT, "> $outfile" ) or die "$!";
   for ( $i = 0; $i <= $#lines; $i++ )
   {
      chomp ( $lines[$i] );

      if ( $lines[$i] =~ /^number:/ )
      {
         $number = $lines[$i];
      }

      if ( $lines[$i] =~ /^name:\s+John/ )
      {
         print OUT "$number\n";
         print OUT "$lines[$i]\n\n";
      }
   }
close ( OUT );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top