CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
extract data...

 

First page Previous page 1 2 Next page Last page  View All


AWHF
Novice

Apr 9, 2007, 7:38 PM

Post #1 of 44 (6605 views)
extract data... Can't Post

hey!

i want to extract word from a file.
the file consist of text.
i want to extract the word in a bracket.
for e.g: pin(Vcc)
the pin( ) in blue color is static while the word in red color is dynamic.
how am i going to wirte the script?


i had try using patter matching
s/\w+/;
if (/pin(/../)/)
print FILEWRITE;
but it failed to function.


osmodius
Novice

Apr 14, 2007, 9:52 AM

Post #2 of 44 (6592 views)
Re: [AWHF] extract data... [In reply to] Can't Post

You're heading to your solution, but you've made a few mistakes. Try:


Code
#!/usr/bin/perl 

open(FILE, "file.txt");
open(FILEWRITE, ">filewrite.txt");
while($line = <FILE>){
if ($line =~ /pin\((\w+)\)/){
print FILEWRITE "$1\n";
}
}
close FILE;
close FILEWRITE;



Need some real-time help? Shove admin@ub3r.net on yer MSN.


AWHF
Novice

Apr 18, 2007, 6:16 PM

Post #3 of 44 (6576 views)
Re: [osmodius] extract data... [In reply to] Can't Post

i have try the script, and its work as what i wanted.
if i want to sort it after extracted? how can i sort it?


osmodius
Novice

Apr 19, 2007, 10:57 AM

Post #4 of 44 (6570 views)
Re: [AWHF] extract data... [In reply to] Can't Post


In Reply To
i have try the script, and its work as what i wanted.
if i want to sort it after extracted? how can i sort it?


You can add the matches to an array, like so..


Code
#!/usr/bin/perl  

open(FILE, "file.txt");
open(FILEWRITE, ">filewrite.txt");
while($line = <FILE>){
if ($line =~ /pin\((\w+)\)/){
push(@matches, "$1");
}
}
close FILE;
close FILEWRITE;


The @matches array will then be populated with all the matching strings between the pin() statement. You can then sort these or do whatever you want with them.


Need some real-time help? Shove admin@ub3r.net on yer MSN.


AWHF
Novice

May 28, 2007, 11:16 PM

Post #5 of 44 (6525 views)
Re: [osmodius] extract data... [In reply to] Can't Post

#!/usr/bin/perl
print "enter name: ";
$name = <>;
open(FILE2,$name);
while(<FILE2>)
{
chomp;
@arr = split;
$pin = @arr[0];
open (FILEREAD, "<$pin");
open (FILEWRITE, ">>result");
while ($line = <FILEREAD>)
{
if ($line =~/pin\((\w+)\)/)
{
push (@matches, "$1\n");
@sorted = sort(@matches);

}
print FILEWRITE "@sorted";
}
close FILEWRITE;
close FILEREAD;
}
close FILE2;




i had try out to sort the data by using the following script but the result it came out is only a few data sorted and not all the data.


KevinR
Veteran


May 28, 2007, 11:48 PM

Post #6 of 44 (6522 views)
Re: [AWHF] extract data... [In reply to] Can't Post

The data is sorted. The problem is that you don't understand how the default sort works. See if this helps you understand how to use the sort() function to get the results you need:

http://perldoc.perl.org/functions/sort.html
-------------------------------------------------


AWHF
Novice

Jun 11, 2007, 8:59 PM

Post #7 of 44 (6505 views)
Re: [KevinR] extract data... [In reply to] Can't Post

Let say, in my home directory I have a directory called “document”. In the directory, all the files name end with .doc (e.g: notes1.doc). The question is how can I write a script to extract the info from all the .doc file and print it in a file (let say the file called “result”). the concept i use is:-
1) open the directory "document"
2) search for all the .doc file (pattern matching)
3) create file "result"
4) extract info from all .doc file
5) print to the extracted info into result

this is the 1st to open the directory before i continuew to next step. is this the correct code?



Code
 #!/usr/bin/perl 
$dirname = "/eng/png/home/awhf/document";
opendir(DIR, "<$dirname")or die "Cannot open directory";
while(<DIR>)
{
.....
.....
.....




KevinR
Veteran


Jun 11, 2007, 10:52 PM

Post #8 of 44 (6501 views)
Re: [AWHF] extract data... [In reply to] Can't Post


In Reply To
Let say, in my home directory I have a directory called “document”. In the directory, all the files name end with .doc (e.g: notes1.doc). The question is how can I write a script to extract the info from all the .doc file and print it in a file (let say the file called “result”). the concept i use is:-
1) open the directory "document"
2) search for all the .doc file (pattern matching)
3) create file "result"
4) extract info from all .doc file
5) print to the extracted info into result

this is the 1st to open the directory before i continuew to next step. is this the correct code?


Code
 #!/usr/bin/perl 
$dirname = "/eng/png/home/awhf/document";
opendir(DIR, "<$dirname")or die "Cannot open directory";
while(<DIR>)
{
.....
.....
.....




Code
#!/usr/bin/perl 
use strict;
use warnings;

my $dirname = "/eng/png/home/awhf/document";
chdir ($dirname) or die "Can't chdir to $dirname: $!";
opendir(DIR, $dirname) or die "Cannot open directory: $!";
my @files = grep {/\.doc$/} DIR;
close DIR;

open (OUT,">>results.txt") or die "Can't open results.txt: $!";
{
local @ARGV = @files;
while (<>) {
print OUT;
}
}
close OUT;

-------------------------------------------------


AWHF
Novice

Jun 11, 2007, 11:57 PM

Post #9 of 44 (6498 views)
Re: [KevinR] extract data... [In reply to] Can't Post

hey!
i had try out your code but there is a error occurs as follow:-

Bareword "DIR" not allowed while "strict subs" in use at script.pl line 8.
Execution of script.pl aborted due to compilation errors.



KevinR
Veteran


Jun 12, 2007, 12:10 AM

Post #10 of 44 (6496 views)
Re: [AWHF] extract data... [In reply to] Can't Post

oops, should be:

my @files = grep {/\.doc$/} readdir DIR;
-------------------------------------------------


AWHF
Novice

Jun 12, 2007, 12:21 AM

Post #11 of 44 (6494 views)
Re: [KevinR] extract data... [In reply to] Can't Post

yeah...!
all the info was print in the result. but the file "result" was alocated in the "document" directory. how if i want to located the file "result" in my home directory?


KevinR
Veteran


Jun 12, 2007, 12:49 AM

Post #12 of 44 (6487 views)
Re: [AWHF] extract data... [In reply to] Can't Post

open (OUT,">>/eng/png/home/awhf/results.txt") or die "Can't open results.txt: $!";
-------------------------------------------------


AWHF
Novice

Jun 12, 2007, 1:30 AM

Post #13 of 44 (6483 views)
Re: [KevinR] extract data... [In reply to] Can't Post

ok! it's work!
this mean the code is to print all the info in the .doc files into the "result". How if i want to print a specific info like what we discuss in the start print and stop print subject where i need to use indicator to indicate when is it "::START" it will start printing and when "::END" it will stop printing. where should i write the code at? is that at "some where here"


Code
 #!/usr/bin/perl  
use strict;
use warnings;
my $dirname = "/eng/png/home/awhf/document";
chdir ($dirname) or die "Can't chdir to $dirname: $!";
opendir(DIR, $dirname) or die "Cannot open directory: $!";
my @files = grep {/\.doc$/}readdir DIR;
close DIR;
open (OUT,">>/eng/png/home/awhf/results.txt") or die "Can't open results.txt: $!";
{
local @ARGV = @files;
while (<>) {
some where here
print OUT;
}
}
close OUT;





KevinR
Veteran


Jun 12, 2007, 10:47 AM

Post #14 of 44 (6475 views)
Re: [AWHF] extract data... [In reply to] Can't Post

maybe:


Code
#!/usr/bin/perl  
use strict;
use warnings;

my $dirname = "c:/";
chdir ($dirname) or die "Can't chdir to $dirname: $!";
opendir(DIR, $dirname) or die "Cannot open directory: $!";
my @files = grep {/\.txx$/} readdir DIR;
close DIR;
print "@files\n";
open (OUT,">results444.txt") or die "Can't open results.txt: $!";
{
local @ARGV = @files;
local $/ = undef;
while (<>) {
if ( /::START(.*?)::END/s ) {
print OUT $1;
}
}
}
close OUT;

-------------------------------------------------


AWHF
Novice

Jun 12, 2007, 11:16 PM

Post #15 of 44 (6461 views)
Re: [KevinR] extract data... [In reply to] Can't Post

i had try out but it can't function as what i want. actually i need to extract specific info fro example:

::Start
Ali
Baba
Zarul
Bahrul
Alec
Foong
Caster
Lee
::End

this is the code that i had try out





Code
  

#!/usr/bin/perl
use strict;
use warnings;
my $dirname = "/eng/png/home/awhf/document";
chdir ($dirname) or die "Can't chdir to $dirname: $!";
opendir(DIR, $dirname) or die "Cannot open directory: $!";
my @files = grep {/\.cas$/} readdir DIR;
close DIR;
print "@files\n";
open (OUT,">>/eng/png/home/awhf/results") or die "Can't open results: $!";
{
local @ARGV = @files;
local $/ = undef;
while (<>)

{
s/\s*(\w+).*/$1/;

if ( /::START(.*?)::END/s )

{

print OUT $1;

}

}

}

close OUT;



KevinR
Veteran


Jun 12, 2007, 11:28 PM

Post #16 of 44 (6454 views)
Re: [AWHF] extract data... [In reply to] Can't Post


Quote
actually i need to extract specific info fro example:


What info are you wanting to extract from the data you posted?
-------------------------------------------------


AWHF
Novice

Jun 12, 2007, 11:37 PM

Post #17 of 44 (6453 views)
Re: [KevinR] extract data... [In reply to] Can't Post

ops...sorry!

::Start
Ali Baba
Zarul
Bahrul
Alec
Foong
Caster
Lee
::End


actually i want to extract the data blue in color


KevinR
Veteran


Jun 13, 2007, 12:05 AM

Post #18 of 44 (6449 views)
Re: [AWHF] extract data... [In reply to] Can't Post

See if this works:


Code
#!/usr/bin/perl     
use strict;
use warnings;
my $dirname = "/eng/png/home/awhf/document";
chdir ($dirname) or die "Can't chdir to $dirname: $!";
opendir(DIR, $dirname) or die "Cannot open directory: $!";
my @files = grep {/\.cas$/} readdir DIR;
close DIR;
print "@files\n";
open (OUT,">>/eng/png/home/awhf/results") or die "Can't open results: $!";
{
local @ARGV = @files;
local $/ = undef;
while(<>) {
if ( /::START(.*?)::END/is ) {
my $match = $1;
$match =~ s/^\s*(\w+).*$/$1/gm;
print OUT $match;
}
}
}
close OUT;

-------------------------------------------------


AWHF
Novice

Jun 13, 2007, 12:46 AM

Post #19 of 44 (6447 views)
Re: [KevinR] extract data... [In reply to] Can't Post

no, it still can't function properly. it print out numbers and not the info which is blue in color


KevinR
Veteran


Jun 13, 2007, 9:33 AM

Post #20 of 44 (6427 views)
Re: [AWHF] extract data... [In reply to] Can't Post

instead of saying: "still can't function properly" describe what the script does do. Does it match some lines? Does it match no lines? Does it give an error?

Try this:


Code
#!/usr/bin/perl      
use strict;
use warnings;
my $dirname = "/eng/png/home/awhf/document";
chdir ($dirname) or die "Can't chdir to $dirname: $!";
opendir(DIR, '.') or die "Cannot open directory: $!";
my @files = grep {/\.cas$/} readdir DIR;
close DIR;
print "@files\n";
open (OUT,">>/eng/png/home/awhf/results") or die "Can't open results: $!";
{
local @ARGV = @files;
local $/ = undef;
while(<>) {
while ( /::START(.*?)::END/isg ) {
my $match = $1;
$match =~ s/^\s*(\w+).*$/$1/gm;
print OUT $match;
}
}
}
close OUT;

-------------------------------------------------


AWHF
Novice

Jun 13, 2007, 8:15 PM

Post #21 of 44 (6417 views)
Re: [KevinR] extract data... [In reply to] Can't Post

As what i had try the code, it still can't function. There is an error. The info in the "result" is full with numbers instead a the info that is in the .doc files.


Code
 #!/usr/bin/perl       
use strict;
use warnings;
my $dirname = "/eng/png/home/awhf/document";
chdir ($dirname) or die "Can't chdir to $dirname: $!";
opendir(DIR, '.') or die "Cannot open directory: $!";
my @files = grep {/\.cas$/} readdir DIR;
close DIR;

open (OUT,">>/eng/png/home/awhf/results") or die "Can't open results: $!";
{
local @ARGV = @files;
local $/ = undef;
while(<>) {
while ( /::START(.*?)::END/isg ) {
my $match = $1;
$match =~ s/^\s*(\w+).*$/$1/gm;
print OUT $match;
}
}
}
close OUT;



the info in the "result" is as follow:
123543678432435476452454769875735
123543579879067563427346582134490
432135468424130978967581341341657

instead of:
Ali
Zarul

Alec

Caster




KevinR
Veteran


Jun 13, 2007, 11:42 PM

Post #22 of 44 (6411 views)
Re: [AWHF] extract data... [In reply to] Can't Post

without seeing the files you are working with I dont know what the problem is.
-------------------------------------------------


AWHF
Novice

Jun 18, 2007, 12:16 AM

Post #23 of 44 (6380 views)
Re: [KevinR] extract data... [In reply to] Can't Post

The example files i work on are as follow:

File1.doc

::Start
Ali Baba
Zarul
Bahrul
Alec
Foong
Caster
Lee
::End

File2.doc

::Start
Calvin Lee
Ester Foong
Alex Wong

Lee Kim
::End

File3.doc

::Start
Jocob Lim
Irene Chai
::End



(This post was edited by AWHF on Jun 18, 2007, 12:22 AM)


AWHF
Novice

Jun 25, 2007, 1:32 AM

Post #24 of 44 (6312 views)
Re: [KevinR] extract data... [In reply to] Can't Post

hey!

this is my code

Code
 #!/usr/bin/perl 
print "enter name: ";
$name = <>;
open(FILE,$name);
while(<FILE>)
{
chomp;
@arr = split;
$file = @arr[0];
open (FILEREAD, "<$file");
open (FILEWRITE, ">>result2");
while (<FILEREAD>)
{
s/\s*(\w+).*/$1/;
if (/^::pin/../^::delay/)
{
print FILEWRITE "$1\n";
} }
close FILEWRITE;
close FILEREAD;
}
close FILE;



instead of using the highlight code how can i automatically open the file without asking the user to keyin?

can i use this code below? how can i combine with my script?

my $dirname = "/engpng/home/awhf/document";
chdir ($dirname) || die "can't chdir to $dirname: $!";
opendir(DIR, $dirname) || die "can't open directory: $!";






KevinR
Veteran


Jun 25, 2007, 9:16 AM

Post #25 of 44 (6308 views)
Re: [AWHF] extract data... [In reply to] Can't Post

opendir() is for opening a directory, not a file. But you could change the path to a file and then use open():


Code
#!/usr/bin/perl 
my $file = 'path/to/some/file.txt';
open(FILE,$file) or die "$!";
while(<FILE>)
{

-------------------------------------------------

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives