|
| Author |
Message |
ScottyGN Newbie

Joined: 12 Feb 2006 Posts: 15
      
|
Posted: Sun Mar 05, 2006 10:10 am Post subject: file output filtering |
|
|
i am looking for code that looks through a file and checks for a certain word and replaces that with another word, then outputs it into a file.
could anyone help me?
PS: i have searched. maybe not good enough, but i did search... |
|
| Back to top |
|
 |
darkmonkey The Merovingian

Joined: 18 Apr 2004 Posts: 2557 Location: London, England
     votes: 7
|
Posted: Sun Mar 05, 2006 11:30 am Post subject: |
|
|
Hello,
Try this:
| Code: | open (FIRST, './original.txt');
my @data = <FIRST>;
close FIRST;
foreach my $data (@data)
{
$data =~ s/hello/goodbye/ig; # Replaces "hello" with "goodbye"
open (NEW, '>> ./new.txt');
print NEW $data."\n";
close NEW;
} |
Change this to what you want:
$data =~ s/hello/goodbye/ig;
"hello" is the word that you want to get rid of, and "goodbye" is the word you want to replace it with.
Hope this helps . _________________ ~ Josh
[ Need bot hosting on a dedicated server? PM me. ] |
|
| Back to top |
|
 |
ScottyGN Newbie

Joined: 12 Feb 2006 Posts: 15
      
|
Posted: Sun Mar 05, 2006 1:48 pm Post subject: |
|
|
| Code: | #made by darkmonkey
#modified slightly by Scoty Grumble
open (FIRST, './original.txt');
my @data = <FIRST>;
close FIRST;
foreach my $data (@data)
{
$data =~ s/<br \/>/\n /ig; # replaces <br /> with \n (enter)
open (NEW, '>> ./converted.pl');
print NEW $data."\n";
close NEW;
} |
Thanks ! now we dont have to do it manually  |
|
| Back to top |
|
 |
Mojave Almost An Agent

Joined: 01 Nov 2003 Posts: 1434
 
|
Posted: Sun Mar 05, 2006 5:14 pm Post subject: |
|
|
| There's no point in opening and closing the output file for each line of of the original... just open it once and close it once - outside the loop. |
|
| Back to top |
|
 |
ScottyGN Newbie

Joined: 12 Feb 2006 Posts: 15
      
|
Posted: Sun Mar 05, 2006 10:59 pm Post subject: |
|
|
i kinda started like started looking innto perl and stuff for ca 1-3 weeks ago ...
so, why dont you tell me how you'd like the file to look ?
Last edited by ScottyGN on Sun Aug 27, 2006 9:21 pm; edited 1 time in total |
|
| Back to top |
|
 |
alienz Almost An Agent

Joined: 22 Mar 2004 Posts: 1436 Location: Mars
 
|
Posted: Wed Mar 08, 2006 10:22 pm Post subject: |
|
|
| ScottyGN wrote: | i kinda started like started looking innto perl and stuff for ca 13 weeks ago ...
so, why dont you tell me how you'd like the file to look ? |
what Mojave means is , take the creating and writing of the new file out of the foreach loop. _________________ Check out Botworld! A dev resource for things bot.
Downloads, articles, news, fourm and more.
http://botworld.marzopolis.com |
|
| Back to top |
|
 |
JTW God Like

Joined: 07 Mar 2004 Posts: 579 Location: Maidstone
  votes: 4
|
Posted: Thu Mar 09, 2006 11:03 am Post subject: |
|
|
I think this is what mojave means As it stops excessive opening and closing of the file everytime you want to change a line.
| Code: |
#made by darkmonkey
#modified slightly by Scoty Grumble
open (FIRST, './original.txt');
my @data = <FIRST>;
close FIRST;
open (NEW, '>> ./converted.pl');
foreach my $data (@data)
{
$data =~ s/<br \/>/\n /ig; # replaces <br /> with \n (enter)
print NEW $data."\n";
}
close NEW;
|
_________________ "Help us, Help you" - BotDepot |
|
| Back to top |
|
 |
alienz Almost An Agent

Joined: 22 Mar 2004 Posts: 1436 Location: Mars
 
|
Posted: Thu Mar 09, 2006 3:50 pm Post subject: |
|
|
Sure, just hand the guy everything he needs...that way he learns how to do nothing for himself... _________________ Check out Botworld! A dev resource for things bot.
Downloads, articles, news, fourm and more.
http://botworld.marzopolis.com |
|
| Back to top |
|
 |
Mojave Almost An Agent

Joined: 01 Nov 2003 Posts: 1434
 
|
Posted: Fri Mar 10, 2006 7:28 am Post subject: |
|
|
| I agree, people need to learn on their own, but this code was so simple, posting it seems ok. |
|
| Back to top |
|
 |
ScottyGN Newbie

Joined: 12 Feb 2006 Posts: 15
      
|
Posted: Sat Mar 11, 2006 2:50 pm Post subject: |
|
|
| i figured out what mojave meant, and, i learnt stuff myself ... btw : its 1-3 not 13 weeks ... |
|
| Back to top |
|
 |
ScottyGN Newbie

Joined: 12 Feb 2006 Posts: 15
      
|
Posted: Tue Mar 14, 2006 12:06 pm Post subject: |
|
|
modified it so that input and output file can be given and so that the file do not open more than once ....
| Code: |
#made by darkmonkey
#modified alot by Scoty Grumble
print("Input Filename:");
my $input = <stdin>;
print("Output Filename:");
my $output = <stdin>;
open (SCOTTY, "<./$input");
my @data = <SCOTTY>;
close SCOTTY;
foreach my $data (@data)
{
$data =~ s/<br \/>/\n /ig; # replaces <br /> with \n (enter)
}
open(FILE, ">./$output");
print FILE "@data\n";
close FILE; |
|
|
| Back to top |
|
 |
alienz Almost An Agent

Joined: 22 Mar 2004 Posts: 1436 Location: Mars
 
|
Posted: Wed Mar 15, 2006 2:29 am Post subject: |
|
|
Cool beginner's script, what is it's purpose? _________________ Check out Botworld! A dev resource for things bot.
Downloads, articles, news, fourm and more.
http://botworld.marzopolis.com |
|
| Back to top |
|
 |
ScottyGN Newbie

Joined: 12 Feb 2006 Posts: 15
      
|
Posted: Sun Aug 27, 2006 9:25 pm Post subject: |
|
|
| lol, replacing the <br /> you find in the forum's commands and functions with \n |
|
| Back to top |
|
 |
|