469,610 Members | 1,779 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

How copy a new line char to a file?

Hi all,

I'm trying to copy a table with a text field column containing a new
line char to a file:

ksDesenv=# create table page(line text) without oids;
CREATE TABLE
ksDesenv=# insert into page (line)
values('1stline'||chr(10)||'2ndline');
INSERT 0 1
ksDesenv=# select * from page;
line
-----------------
1stline
2ndline
(1 registro)

ksDesenv=# copy page to '/var/www/html/kakaostats/page.txt';
COPY

But this is what the page.txt file looks like:

1stline\n2ndline

What can I do to make the copy to command insert an actual new line
char?

Regards,
Clodoaldo Pinto Neto

__________________________________________________ _____
Yahoo! Acesso Grátis - navegue de graça com conexão de qualidade! Acesse: http://br.acesso.yahoo.com/

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
2 4459
Hi all,

I should have explained it better:

Why don't I just insert each line in the table and let "copy to" do its
thing inserting a new line char (lf) at the end of each line?
Because I need the file lines to be in a certain order. Copy to will
copy the lines in its own order.
Until now I'm inserting html pages in a single line table without a lf
and copying then to files. Here is the result:
http://planeta.terra.com.br/informatica/kakaostats/
http://planeta.terra.com.br/informat...ts/t13802.html
Right click the page and click view source and there it is, a single
giant line.
The problem with mixing html and pl/pgsql is that it becomes a big
mess. It is very hard to evolve the code and understand it.
So I choose to not output html anymore but cvs text and build the page
with php. Advantages: much simpler pl/pgsql code and upload size 80%
smaller.
Lines finishing with a lf are much easier to parse in php (fgetcsv).
This and the need to have then ordered is why i need to insert lfs in
the single line table from which to copy to file.
I have solved it with a simple python script but it is one more step
and is not fail safe. Where there would be a lf I insert the string
"newLine". Then in the python script I change it to an actual lf char.
But in the remote possibility that there is or there will be this
string in the database (600+ thousand distinct user names) then the
output file will be wrong.
Also it is one more piece of software to be mantained.

Regards,
Clodoaldo Pinto

--- Clodoaldo Pinto Neto <cl*************@yahoo.com.br> escreveu:
Hi all,

I'm trying to copy a table with a text field column containing a new
line char to a file:

ksDesenv=# create table page(line text) without oids;
CREATE TABLE
ksDesenv=# insert into page (line)
values('1stline'||chr(10)||'2ndline');
INSERT 0 1
ksDesenv=# select * from page;
line
-----------------
1stline
2ndline
(1 registro)

ksDesenv=# copy page to '/var/www/html/kakaostats/page.txt';
COPY

But this is what the page.txt file looks like:

1stline\n2ndline

What can I do to make the copy to command insert an actual new line
char?

Regards,
Clodoaldo Pinto Neto

__________________________________________________ _____
Yahoo! Acesso Grátis - navegue de graça com conexão de qualidade!
Acesse: http://br.acesso.yahoo.com/

---------------------------(end of
broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to
ma*******@postgresql.org


__________________________________________________ _____
Yahoo! Acesso Grátis - navegue de graça com conexão de qualidade! Acesse: http://br.acesso.yahoo.com/

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #2

On Wed, 11 Aug 2004, [iso-8859-1] Clodoaldo Pinto Neto wrote:
I should have explained it better:

Why don't I just insert each line in the table and let "copy to" do its
thing inserting a new line char (lf) at the end of each line?
Because I need the file lines to be in a certain order. Copy to will
copy the lines in its own order.
Until now I'm inserting html pages in a single line table without a lf
and copying then to files. Here is the result:
http://planeta.terra.com.br/informatica/kakaostats/
http://planeta.terra.com.br/informat...ts/t13802.html
Right click the page and click view source and there it is, a single
giant line.
The problem with mixing html and pl/pgsql is that it becomes a big
mess. It is very hard to evolve the code and understand it.
So I choose to not output html anymore but cvs text and build the page
with php. Advantages: much simpler pl/pgsql code and upload size 80%
smaller.
Lines finishing with a lf are much easier to parse in php (fgetcsv).
This and the need to have then ordered is why i need to insert lfs in
the single line table from which to copy to file.
I have solved it with a simple python script but it is one more step
and is not fail safe. Where there would be a lf I insert the string
"newLine". Then in the python script I change it to an actual lf char.
But in the remote possibility that there is or there will be this
string in the database (600+ thousand distinct user names) then the
output file will be wrong.
Also it is one more piece of software to be mantained.


I think in general you might be better off either writing functions in an
untrusted language that actually do what you want with the files or a
"client" app that connects to the database and gets the data you want and
puts it into a file. I don't think COPY TO is really meant as a general
purpose writing arbitrary content into a file tool.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Joker | last post: by
11 posts views Thread by David Morgenthaler | last post: by
11 posts views Thread by elrondrules | last post: by
7 posts views Thread by Simon van Beek | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.