Current location: Hot Scripts Forums » Programming Languages » Perl » running code against contents of a file?


running code against contents of a file?

Reply
  #1 (permalink)  
Old 09-26-06, 06:14 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
running code against contents of a file?

I am trying to extract text from a file based on conditions and I am a noob so I am asking for help again.

I dont need help with setting the conditions right now but rather how do I write a perl script that acts upon the contents of a seperate file and outputs to a new file?

Ex. script name is script.pl and text to read and act upon is in file.txt and output file is output.txt.
All files are in the same directory.

Thanks again for your help,

-vbsaltydog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-26-06, 07:48 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
I figured out how to access the file contents with the open and close functions and I got the lines of text stored into an array and printed them out using a foreach statement but now how do I get it to only print text that matches a pattern? and how do I output the results to a file?

Thank to everyone for your help.

-vbsaltydog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-26-06, 10:07 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
please post the code you have so far. Explain what the considtions are. Post some sample data if possible.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-26-06, 10:51 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
The goal is to read the code in the file being opened and output only the text strings that match the regular expression of /<b>anything</b>/ to a new file.

Here is the current code:
__________________________________________________ __________
Code:
#!/usr/bin/perl

open(SOURCE,      "file") || die("can't open file: $!");

@array = <SOURCE>;

close SOURCE;

foreach $line (@array) {

print $line;

} # End of foreach.
__________________________________________________ ________

Thanks for your help,

-vbsaltydog

Last edited by nico_swd; 09-27-06 at 03:27 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 09-26-06, 11:11 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
one possibility:

Code:
#!/usr/bin/perl
open(IN, "infile") or die "can't open infile: $!";
open(OUT, ">outfile") or die "can't open outfile: $!";
@array = <IN>;
close(IN);
foreach $line (@array) {
   print OUT $line if ($line =~ /<b>.*?<\/b>/);
}
close(OUT);
if the input file is too big to fit into memory:

Code:
#!/usr/bin/perl
open(IN, "infile") or die "can't open infile: $!";
open(OUT, ">outfile") or die "can't open outfile: $!";
while(my $line = <IN>) {
   print OUT $line if ($line =~ /<b>.*?<\/b>/);
}
close(IN);
close(OUT);
or as a command line / one liner:

Code:
perl -n -e "print if /<b>.*?<\/b>/" infile >outfile

Last edited by Drunken Perl Coder; 09-26-06 at 11:13 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 09-27-06, 12:29 AM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the response. It worked well.
How can I set the regex to match no more than 5 words?

-vbsaltydog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 09-27-06, 01:38 AM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by vbsaltydog
Thanks for the response. It worked well.
How can I set the regex to match no more than 5 words?

-vbsaltydog

Explain better please. Post some sample data and show what you want the results to be.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 09-27-06, 05:11 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Its OK, I figured out how to do it by using the [0,50] inside the regex so it matches at least one character but not more than 50 characters.

Your willingness to help is appreciated.

-vbsaltydog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form to Text File Justin171985 PHP 14 02-09-11 12:55 PM
HTML code file update on the webserver using CGI script sujata_ghosh Perl 31 07-06-06 04:06 AM
File Copying and rename script soundman87 Script Requests 3 05-23-06 12:47 PM
create php file on the fly and delete when session ends recedo PHP 0 01-06-06 11:28 PM
Simple Create a file code ancdy Script Requests 2 01-24-05 12:02 PM


All times are GMT -5. The time now is 02:01 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.