Gossamer Forum
Home : Products : DBMan : Customization :

dbman and grep / awk?

Quote Reply
dbman and grep / awk?
Hi Guys, I'm using dbman to edit a large database file and it works slick. Now I'd like to teach it another trick. The database file is the master product file for a shopping cart cgi program. When something goes "sold-out" we use dbman to add a graphic to the name of the item. Hence, "Blue Gloves" become "Blue Gloves<img src=SOLD_OUT.gif>" (We just paste the tag info into the name field behind the item name)

This works just fine. Now for the new trick.

We allow people to add items to a shopping cart and return to the store at a later date. The cart stores the info in a simple text file, with a unique file name for each shopper, in a special directory. What I'd like to do is have dbman make the changes to the master product list, as it is already doing, but then, using grep or awk or something like that, search this cart directory and replace all "blue gloves" with "blue gloves<img src=SOLD_OUT.gif>" I'd like it to mirror the changes we make to the main product data base file.

The idea is to allow people to add items to the shopping cart over time, but if something in their cart is sold out before they finalize the order, then I'd like that updated in the cart file.

It seems like it should be pretty simple, smile, but I'm pretty simple as well and haven't been able to sort this. Thanks in advance for any thoughts or suggestions.
Quote Reply
Re: [steven99] dbman and grep / awk? In reply to
I think it's actually the sed command that I want. Populated with my variables from the Edit form from dbman. I don't really care if it's a bit slow...can I get dmban to first edit it's own file, then do a subroutine that will run my sed command?

Sed works as below (quote):

We won't discuss all the flags yet. The one we use below is g which means "replace all matches"

>cat file
I have three dogs and two cats
>sed -e 's/dog/cat/g' -e 's/cat/elephant/g' file
I have three elephants and two elephants
>



Quote Reply
Re: [steven99] dbman and grep / awk? In reply to
Add the subroutine to the dbman script (probably under db.cgi ?) and then call the subroutine at the bottom of "modify_success" or any of the "_success" subs: add_success, modify_success, delete_success, etc. These are what indicate that dbman script has "finished" running it's part.

This site has some good Perl snippets of code:
http://www.generalhosting.com/scripts/perl-scripts.htm
(notice the section called "replace") - It's a lot more complicated than what I use though.

Here is what I use on Windows to replace text in a type of file (.txt, .js, .htm) in a particular directory.

Code:
chdir('F:\Temp') or die "cannot connect to F: \n";

@AllDTA = glob('*.csv'); #type of file to search for

foreach $FileName (@AllDTA) {

my $notelines;

open(NOTES, "<$FileName") or die "Could not open $FileName for reading\n";
while (<NOTES>) {
$notelines .= $_;
}
close(NOTES);

open(NOTES, ">$FileName") or die "Cannot find $FileName - no such file or invalid path\n";
select(NOTES);

$notelines =~ s/Bob/Joe/g; #what to search and replace on

print $notelines;

close(NOTES);

}

You'd have to change the chdir command to work on Unix (may be the same?) if you're on Unix.

Last edited by:

Watts: Mar 14, 2007, 12:18 PM
Quote Reply
Re: [Watts] dbman and grep / awk? In reply to
thanks for getting back to me Watts. And a good answer to boot. I haven't had time to look at the cgi snippits.

It is for Unix, and now I've found something call "treesed" which does all the adding and changing of your remote files. I've been able to have it effect the changes I want in that directory by running treesed from the command line.

So, now I want to hook up with my variables from db.cgi, as you mentioned, maybe in sub_add_record - right after html_add_success (see below)

# We keep checking for the next available key, or until we've tried 50 times
# after which we give up.
while ($status eq "duplicate key error" and $db_key_track) {
return "duplicate key error" if ($counter++ > 50);
$in{$db_key}++;
$status = &validate_record;
}

if ($status eq "ok") {
open (DB, ">>$db_file_name") or &cgierr("error in add_record. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB &join_encode(%in);
close DB; # automatically removes file lock
if ($db_key_track) {
open (ID, ">$db_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) {
flock(ID, 2) or &cgierr("unable to get exclusive lock on $db_id_file_name.\nReason: $!");
}
print ID $in{$db_key}; # update counter.
close ID; # automatically removes file lock
}
&auth_logging("added record: $in{$db_key}") if ($auth_logging);
&html_add_success;
}
else {
&html_add_failure($status);
}
}




***********
***********

Okay, this is one of the variables, right? $in{$db_key}

Thank, steven
Quote Reply
Re: [steven99] dbman and grep / awk? In reply to
Thats the key variable... You can try $in{'item'} or $in{'ID'} with the part betweent the ' ' being the field name.

You may want to create a subroutine in html.pl and then under html_add_success (also located in html.pl) call it by adding &whatever_you_name_it; at the bottom of the add success sub. Try using $in{''} or $rec{''} depending on where in the script you are calling the subroutine (at the top of the sub you'll see something like "my (%rec) = @_;" which will indicate you should call your variable $rec{'item'}.