[thelist] bash, read from several file

Dan CRACIUN dcsquare at myrealbox.com
Sat Apr 17 01:17:54 CDT 2004


I think I'm a little lost here: do you want to populate a table or 
several tables? If it's a table and you have the fields in different 
files, the php solution is bellow (it's untested and needs error 
trapping, but it should get you started)

$hnd1 = fopen("file1", "r");
$hnd2 = fopen("file2", "r");
---------------------------
$hndk=fopen("filek", "r");
|$dbcon = pg_connect("host=some_host port=your_port dbname=database_name 
user=some_user password=foo");|
while (!feof($hnd1)) {
  $buffer1 = |fgets($hnd1, 4096);|
|  ----------------------------|
|  $bufferk = fgets($hndk, 4096);|
|  pg_query($dbcon, "INSERT into contact VALUES(\'$buffer1\', 
\'$buffer2\', -------, \'$bufferk\')");|
||}

Cheers,
Dan CRACIUN

Magnus Østergaard wrote:

>Hi,
>
>I am trying to populate a postgresql db with values from several files.
>In each file, each line correspont to the same line number in the other
>files.
>The problem is how do I make my loop?
>
>This is what I've got so far:
>
>#!/bin/sh
>while read name;
> do psql -d nobody nobody -c "INSERT INTO contact(name) VALUES('$name');" ;
>done< ./name.txt
>
>But this only reads one file.
>
>How can I read from two files (or more) at one time. Say name.txt and
>email.txt and then do.
>
>"INSERT INTO contact(name,email) VALUES('$name','$email');"
>
>I am looking for a bash solution, but a php one could help me.
>
>// Magnus
>
>
>  
>



More information about the thelist mailing list