Gossamer Forum
Home : General : Perl Programming :

edit a word in a flat file

Quote Reply
edit a word in a flat file
Im having problems figuring out this.

I have a flat file that has a list and each line goes like this:

number/user/name/type/thing/place

what if I just want to edit the value of thing? I can read the file and check against it but I cant figure out a way to just edit one value

I cant seem to thiink of a way to do that

thanks

lain
Quote Reply
Re: [wired_lain] edit a word in a flat file In reply to
Its no where near as easy as with SQL, but here is an example bit of code (there IS better out there...Wink)

Code:
my $find = "something"; # the word you want to replace...

open(FILE, "/path/to/file/readig.txt") || die "Cant
open(NEWFILE, "/path/to/file/to/write-to.txt") || die "Cant open. Reason: $!";

while (<FILE>) {

chomp;
my ($number,$user,$name,$type,$thing,$place) = split("/",$_);

if ($thing eq $find) {

print NEWFILE "/$number/$user/$name/$type/$find/$place\n";

} else {

print NEWFILE $_ . "\n";

}


}

close(FILE);
close(NEWFILE);

This code is UNTESTED, I just quickly wrote it before going out clubbing Wink

Enjoy :)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] edit a word in a flat file In reply to
Thanks Andy, you were a big help, heres what I came up with

sub edit_pass{
open (PASS, "<$pwd") or &cgierr ("unable to open: user.db.\nReason: $!");
@lines = <PASS>;
@lines = split(/\n/);
close PASS;

open (PASS, ">docno.log") || die "CANT";
foreach $line (@lines) {
chomp;
my ($user_no, $name_jap, $name_eng, $loginname, $passwd, $root) = split(/::/, $line);
$root =~ s/[^0-9a-z\/]//gi;
# if ($passwd eq $oldpass){
if ($line =~ /^$oldpass::/) {
my $newline .= join("::", $user_no, $name_jap, $name_eng, $loginname, $newpass1, $root);
print PASS "$newline\n";
}
else {
print PASS $line . "\n";
}}
close (PASS);
}

changing the distinct part of the line is now working but the problem is I end up with deleting all the other items in the document except for the line that I changed. I cant figure out whats wrong. thanks so much

Lain

Last edited by:

wired_lain: Oct 27, 2002, 10:39 PM
Quote Reply
Re: [wired_lain] edit a word in a flat file In reply to
open (PASS, ">docno.log") || die "CANT";

to

open (PASS, ">>docno.log") || die "CANT";

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] edit a word in a flat file In reply to
If I do that then isnt the line that I am suppose to edit not going to be erased? the other lines are saved but so is the one I am editing, is there any way to go around this?
Quote Reply
Re: [wired_lain] edit a word in a flat file In reply to
Nope...

>> is to add something to a file
> is to overwrite the old data, and add the new stuff

Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] edit a word in a flat file In reply to
I got the script to work yay!

thanks Andy