User Control Panel
Advertisements

HELP US, HELP YOU!

file output filtering

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
ScottyGN
Newbie
Newbie


Joined: 12 Feb 2006
Posts: 15

Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8

PostPosted: Sun Mar 05, 2006 10:10 am    Post subject: file output filtering Reply with quote

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
The Merovingian


Joined: 18 Apr 2004
Posts: 2557
Location: London, England
Reputation: 39.3Reputation: 39.3Reputation: 39.3Reputation: 39.3
votes: 7

PostPosted: Sun Mar 05, 2006 11:30 am    Post subject: Reply with quote

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 Smile.

_________________
~ Josh
[ Need bot hosting on a dedicated server? PM me. ]
Back to top
ScottyGN
Newbie
Newbie


Joined: 12 Feb 2006
Posts: 15

Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8

PostPosted: Sun Mar 05, 2006 1:48 pm    Post subject: Reply with quote

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 Very Happy
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Sun Mar 05, 2006 5:14 pm    Post subject: Reply with quote

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
Newbie


Joined: 12 Feb 2006
Posts: 15

Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8

PostPosted: Sun Mar 05, 2006 10:59 pm    Post subject: Reply with quote

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
Almost An Agent


Joined: 22 Mar 2004
Posts: 1436
Location: Mars
Reputation: 55.7

PostPosted: Wed Mar 08, 2006 10:22 pm    Post subject: Reply with quote

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
God Like


Joined: 07 Mar 2004
Posts: 579
Location: Maidstone
Reputation: 67.1
votes: 4

PostPosted: Thu Mar 09, 2006 11:03 am    Post subject: Reply with quote

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
Almost An Agent


Joined: 22 Mar 2004
Posts: 1436
Location: Mars
Reputation: 55.7

PostPosted: Thu Mar 09, 2006 3:50 pm    Post subject: Reply with quote

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
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Fri Mar 10, 2006 7:28 am    Post subject: Reply with quote

I agree, people need to learn on their own, but this code was so simple, posting it seems ok.
Back to top
ScottyGN
Newbie
Newbie


Joined: 12 Feb 2006
Posts: 15

Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8

PostPosted: Sat Mar 11, 2006 2:50 pm    Post subject: Reply with quote

i figured out what mojave meant, and, i learnt stuff myself ... btw : its 1-3 not 13 weeks ...
Back to top
ScottyGN
Newbie
Newbie


Joined: 12 Feb 2006
Posts: 15

Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8

PostPosted: Tue Mar 14, 2006 12:06 pm    Post subject: Reply with quote

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
Almost An Agent


Joined: 22 Mar 2004
Posts: 1436
Location: Mars
Reputation: 55.7

PostPosted: Wed Mar 15, 2006 2:29 am    Post subject: Reply with quote

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
Newbie


Joined: 12 Feb 2006
Posts: 15

Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8Reputation: 6.8

PostPosted: Sun Aug 27, 2006 9:25 pm    Post subject: Reply with quote

lol, replacing the <br /> you find in the forum's commands and functions with \n
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group