Gossamer Forum
Home : General : Perl Programming :

Need help

Quote Reply
Need help
Hello,

I have a text file that has a series of records each with several fields just like a links2.0 database. However, there are no ID numbers records. I need to import these records into a links2.0 database and hence I need to add ID numbers to each record first.

e.g.
|field1|field2|....field13|field14|
|field1|field2|....field13|field14|
|field1|field2|....field13|field14|

I need to start the ID for the first record with 1000 and then 1001, 1002 and so on.

so it would be...
1001|field1|field2|....field13|field14|
1002|field1|field2|....field13|field14|
1003|field1|field2|....field13|field14|

thanks in advance



Quote Reply
Re: Need help In reply to
Try:

my $i = 1000;
open DB, "</path/to/database.db";
open NEW, ">/path/to/new.db";
while (<DB>) {
$i++;
print NEW "$i|$_\n";
}
close NEW
close DB;



Quote Reply
Re: Need help In reply to
Might want to open in append mode rather than write mode. (just in case new.db is set to the current links database) Smile

open NEW, ">>/path/to/new.db";

- Mark

Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: Need help In reply to
Thats why I entered new.db so it wouldn't overwrite links.db :)

........and if you set it to >> it will cause an internal server error when run because it can't open the file for writing


Quote Reply
Re: Need help In reply to
 
Paul,

Thanks a lot. But I have a new issue. Turns out some of the records have more than 14 fields and some have less than 14 fields.

Is there a way to move records with more or less than 14 fields into a new database so I can take a look and edit them manually.

thanks agian.

Quote Reply
Re: Need help In reply to
Hi,

Try this modification to Paul's script:
Code:

my $i = 1000;
open DB, "</path/to/database.db";
open NEW, ">/path/to/new.db";
open ERRORS, ">/path/to/errors.db";
while (<DB>) {
@fields = split(/\|/, $_);
if($#fields == 13) {
$i++;
print NEW "$i|$_";
}
else {
print ERRORS "$_";
}
}
close ERRORS;
close NEW;
close DB;



(This has not been tested.)

This will only increment the records with 14 fields. Other records will go into the errors.db with no number appended to the front.

Note also I removed the newline character from the output (\n), otherwise you'll probably get a blank line between each record.

Hope that helps,

Matt G

Last edited by:

mglaspie: Sep 10, 2001, 2:28 PM
Quote Reply
Re: Need help In reply to
You can change

@fields = split(/\|/, $_);

to

@fields = split /\|/;

This may be another approach:
Code:
my $i = 1000;
my $tot;
open DB, "</path/to/database.db";
open NEW, ">/path/to/new.db";
open ERRORS, ">/path/to/errors.db";
while (<DB>) {
$i++;
$tot = ($_ =~ tr/\|//);
if ($tot > 13) {
print NEW "$i|$_\n";
} else {
print ERRORS "$_\n";
}
}
close ERRORS;
close NEW;
close DB;

Again not tested.

Last edited by:

PaulWilson: Sep 10, 2001, 2:33 PM
Quote Reply
Re: Need help In reply to
Paul,

Won't this line:
$tot = ($_ =~ tr/\|//);
actually strip out all the "|" characters from $_?

What about this instead?:
$tot = ($_ =~ tr/\|/\|/);

Also, this line:
if ($tot > 13) {

Would probably be better as
if ($tot == 14) {

...since it looks like Socrates wants to manually look at all records that don't contain exactly 14 fields.

Any ideas on which method would be faster?
(@fields=split// v. tr///)? (Not that it would matter for a small file, but just curious.)


--
Matt G
Quote Reply
Re: Need help In reply to
Quote:
Won't this line:
$tot = ($_ =~ tr/\|//);
actually strip out all the "|" characters from $_?

Nope, it counts the number of |'s on each line.

If there are more than 13 it means there are 14 or more fields so that line needs to go in new.db. If $tot is less that 13 then it needs to go in error.db

tr/// is not the same as s///


Last edited by:

PaulWilson: Sep 10, 2001, 2:59 PM
Quote Reply
Re: Need help In reply to
Quote:
Thats why I entered new.db so it wouldn't overwrite links.db :)
I just had visions of people coming back and saying "I ran the script but it wiped my database!" ;)

Quote:
........and if you set it to >> it will cause an internal server error when run because it can't open the file for writing
No it won't... If the file doesn't exist it will just create one as normal, otherwise it will open the existing file and append instead.

- Mark

Astro-Boy!!
http://www.zip.com.au/~astroboy/

Last edited by:

AstroBoy: Sep 10, 2001, 6:11 PM
Quote Reply
Re: Need help In reply to
Ooops....

close NEW

should be

close NEW;

Yep Mark you are correct....don't know what I was blabbin on about.


Quote Reply
Re: [PaulWilson] Need help In reply to
Ok, all worked well. Thank you all.

Any ideas on how to import the fixed data from the error file back in to the good data file, and sort all by ID's.