Thread: How would I get rid of trailing blank line?

How would I get rid of trailing blank line?

From
"Tena Sakai"
Date:
<p><font size="2">Hi Everybody,<br /><br /> I am using postgres 8.3.4 on linux.<br /> I often use a line like:<br />  
psql-tf query.sql mydatabase > query.out<br /><br /> -t option gets rid of the heading and count<br /> report at the
bottom. There is a blank line<br /> at the bottom, however.  Is there any way to<br /> have psql not give me that blank
line?<br/><br /> Thank you for your help.<br /><br /> Regards,<br /><br /> Tena Sakai<br /> tsakai@gallo.ucsf.edu<br
/></font>

Re: How would I get rid of trailing blank line?

From
Tom Lane
Date:
"Tena Sakai" <tsakai@gallo.ucsf.edu> writes:
> I often use a line like:
>   psql -tf query.sql mydatabase > query.out

> -t option gets rid of the heading and count
> report at the bottom.  There is a blank line
> at the bottom, however.  Is there any way to
> have psql not give me that blank line?

Doesn't look like it --- the final fputc('\n', fout); seems to be
done unconditionally in all the output formats.  I wonder if we should
change that?  I'm afraid it might break programs that are used to it :-(
        regards, tom lane


Re: How would I get rid of trailing blank line?

From
"Tena Sakai"
Date:
<p><font size="2">Hi Tom,<br /><br /> I am a bit surprised to hear that that '\n'<br /> is there unconditionally.  But
Iam sure<br /> there are more pressing things for you to<br /> work on.  It's something I can live with.<br /><br />
Regards,<br/><br /> Tena Sakai<br /> tsakai@gallo.ucsf.edu<br /><br /><br /> -----Original Message-----<br /> From: Tom
Lane[<a href="mailto:tgl@sss.pgh.pa.us">mailto:tgl@sss.pgh.pa.us</a>]<br /> Sent: Thu 4/2/2009 4:01 PM<br /> To: Tena
Sakai<br/> Cc: pgsql-sql@postgresql.org; pgsql-hackers@postgresql.org<br /> Subject: Re: [SQL] How would I get rid of
trailingblank line?<br /><br /> "Tena Sakai" <tsakai@gallo.ucsf.edu> writes:<br /> > I often use a line
like:<br/> >   psql -tf query.sql mydatabase > query.out<br /><br /> > -t option gets rid of the heading and
count<br/> > report at the bottom.  There is a blank line<br /> > at the bottom, however.  Is there any way to<br
/>> have psql not give me that blank line?<br /><br /> Doesn't look like it --- the final fputc('\n', fout); seems
tobe<br /> done unconditionally in all the output formats.  I wonder if we should<br /> change that?  I'm afraid it
mightbreak programs that are used to it :-(<br /><br />                         regards, tom lane<br /><br /></font> 

Re: How would I get rid of trailing blank line?

From
Scott Marlowe
Date:
On Thu, Apr 2, 2009 at 3:33 PM, Tena Sakai <tsakai@gallo.ucsf.edu> wrote:
> Hi Everybody,
>
> I am using postgres 8.3.4 on linux.
> I often use a line like:
>   psql -tf query.sql mydatabase > query.out
>
> -t option gets rid of the heading and count
> report at the bottom.  There is a blank line
> at the bottom, however.  Is there any way to
> have psql not give me that blank line?

Tired of those blank lines in your text files?  Grep them away:

psql -tf query.sql mydatabase | grep -v "^$" > query.out


>
> Thank you for your help.
>
> Regards,
>
> Tena Sakai
> tsakai@gallo.ucsf.edu
>



--
When fascism comes to America, it will be the intolerant selling it as
diversity.


Re: [HACKERS] How would I get rid of trailing blank line?

From
"Tena Sakai"
Date:
<p><font size="2">Hi Andrew,<br /><br /> > Right. There's a simple pipeline way to get rid of it:<br /> >   psql
-t-f query.sql | sed -e '$d' > query.out<br /><br /> Hi Scott,<br /><br /> > Tired of those blank lines in your
textfiles?  Grep them away:<br /> > psql -tf query.sql mydatabase | grep -v "^$" > query.out<br /><br /> Thank
youBoth.<br /><br /> Regards,<br /><br /> Tena Sakai<br /> tsakai@gallo.ucsf.edu<br /><br /><br /> -----Original
Message-----<br/> From: Andrew Dunstan [<a href="mailto:andrew@dunslane.net">mailto:andrew@dunslane.net</a>]<br />
Sent:Thu 4/2/2009 6:34 PM<br /> To: Tom Lane<br /> Cc: Tena Sakai; pgsql-sql@postgresql.org;
pgsql-hackers@postgresql.org<br/> Subject: Re: [HACKERS] [SQL] How would I get rid of trailing blank line?<br /><br
/><br/><br /> Tom Lane wrote:<br /> > "Tena Sakai" <tsakai@gallo.ucsf.edu> writes:<br /> >  <br /> >>
Ioften use a line like:<br /> >>   psql -tf query.sql mydatabase > query.out<br /> >>    <br /> ><br
/>>  <br /> >> -t option gets rid of the heading and count<br /> >> report at the bottom.  There is a
blankline<br /> >> at the bottom, however.  Is there any way to<br /> >> have psql not give me that blank
line?<br/> >>    <br /> ><br /> > Doesn't look like it --- the final fputc('\n', fout); seems to be<br />
>done unconditionally in all the output formats.  I wonder if we should<br /> > change that?  I'm afraid it might
breakprograms that are used to it :-(<br /> ><br /> ><br /> >  <br /><br /> Right. There's a simple pipeline
wayto get rid of it:<br /><br />     psql -t -f query.sql | sed -e '$d' > query.out<br /><br /><br /> cheers<br
/><br/> andrew<br /><br /></font> 

Re: How would I get rid of trailing blank line?

From
"Greg Sabino Mullane"
Date:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160


> report at the bottom.  There is a blank line
> at the bottom, however.  Is there any way to
> have psql not give me that blank line?

Now that my presenation on psql is over :), I'll share my solution:

psql -AX -qt -c "SELECT ..." | perl -pe 's/^\n// if $.<2'

This strips a newline from the first line only of the output, and
only if the line consists of nothing else. Highly recommended
for cron.

- --
Greg Sabino Mullane greg@turnstep.com
End Point Corporation
PGP Key: 0x14964AC8 200904052221
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

iEYEAREDAAYFAknZZ0YACgkQvJuQZxSWSsiDAwCglJS9/juQLe8asY3sG9fagbeo
2V4An0p5U6UHGI1KXoe2qQvURX5E5BZo
=Yy0J
-----END PGP SIGNATURE-----




Re: How would I get rid of trailing blank line?

From
Jasen Betts
Date:
On 2009-04-02, Tena Sakai <tsakai@gallo.ucsf.edu> wrote:

> I am using postgres 8.3.4 on linux.
> I often use a line like:
>   psql -tf query.sql mydatabase > query.out
>
> -t option gets rid of the heading and count
> report at the bottom.  There is a blank line
> at the bottom, however.  Is there any way to
> have psql not give me that blank line?

I ask postgres to format it for me instead of relying on psql

psql db_name  -c "copy (select * from foo ) to stdout;" 

this gives me postgres style tab separated values, but I can 
have CSV (or any otther format COPY can do) if I want.
I usually use a more complex subquery with a list of columns and 
expressions, where,  order by clauses, etc... )

I use this in a script that pulls data from one database and inserts
in into another.

Requires postgres 8.2 or later




Re: How would I get rid of trailing blank line?

From
"david.paulo"
Date:
The sed -e '$d' is nice but it has a downside - it will mask the return code
from psql in the case when there is an error



--
View this message in context:
http://postgresql.nabble.com/How-would-I-get-rid-of-trailing-blank-line-tp2153908p5873931.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.