LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2007, 01:43 PM   #1
Anonycat
LQ Newbie
 
Registered: Jan 2006
Distribution: Fedora
Posts: 6

Rep: Reputation: 0
Need a certain command for a Perl script (Irssi)


I'm working on a script that's currently one step away from being completed, but I don't know how to enter a command capable of performing the final step.

The script manages a text file containing a list of member names from a website, as well as all IRC names that are tied to those members. I'm able to detect whether or not a given nick or member name is already present in the file, and upon "registration" with the IRC bot, if neither name is already in the file, a line is added correctly. However, it's the case when the member name is already present, and the line in the middle of the file needs to be modified, that I can't figure out.

For simplicity, let's say the file is located at /home/foo/bar.txt. It's tab-delimited, with the site member name as the first token on a line, followed by a single pipe | if the name is open for additional links or two pipes || if the name has been "locked" by the user for added security against someone potentially trying to link new names and impersonate them (this shouldn't be an issue anyway, but it's something that will let them feel safer), and then each of the IRC names that's linked to that member name.

$reg_name and $used_nick are variables that, at this point, contain the desired values. I want to perform "s/$reg_name\t\|\t/$reg_name\t\|\t$used_nick\t/" on /home/foo/bar.txt, so that the file is actually modified and in the correct way. It would be preferred if the s/ statement could be applied to the entire file at once, instead of doing s/ on each individual line and then putting it back in the file in such a way that it doesn't overwrite the full contents of the file with that single line, or append it to the end while leaving the original line intact (but just having something that works is preferable to the alternative). Under no circumstances should there be more than one match, so s/.../.../g isn't necessary. I know what I want to do, but can't figure out how to implement it correctly in a Perl script...any ideas?

Last edited by Anonycat; 05-21-2007 at 01:47 PM.
 
Old 05-22-2007, 05:54 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,450

Rep: Reputation: 209Reputation: 209Reputation: 209
er, examples would be nice.

but for inplace perl edits of a file use:
Code:
perl -pi.bak -e 's/THIS/THAT/'  file
creates a .bak backup

(you can set the $^I variable in the script if you prefer
 
Old 05-22-2007, 11:21 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Or, if your database is not large (probably a few thousand records or less), and your traffic levels are not high, simply swallow the database into memory as a hash, and then re-write it to disk after modification. This can become quite fast and simple if you put the member names and member IRCs on alternating lines of your database file.

Code:
open(MEMBERS, "/home/foo/bar.txt" );
my %memberDb = <MEMBERS>;
close MEMBERS;

#
#  Add, delete, modify member records....
#

open( MEMBERS,">/home/foo/bar.txt" );
print MEMBERS %memberDb;
close MEMBERS;
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting PID from a system command in Linux perl script sumbabs Linux - General 1 03-03-2005 10:38 AM
Problems with perl and irssi-scripts.. magoot Programming 0 02-27-2005 05:24 AM
PERL script OK at command line, not in browser alvo Programming 4 12-19-2004 08:28 AM
Irssi and perl and scripts? MrDoctor Linux - Newbie 0 04-05-2004 09:44 AM
newgrp command within perl/csh script bobsey Programming 1 04-13-2001 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration