psql output without return \n


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting psql output without return \n
# 1  
Old 04-08-2010
Question psql output without return \n

Hi Everyone,

When i finish running
Code:
[root@tmp]# echo `psql -t -U root databaseA -c "select a, b from book"`;

i get the output
Code:
107275 | 14 107301 | 2 107446 | 6 107820 | 77 107929 | 101

Would like to have the result like:
Code:
107275 | 14
107301 | 2
107446 | 6
107820 | 77
107929 | 101

or can i use printf, sprintf, or awk, etc, to make the output like this? Please advice.

Thanks
Regards,
# 2  
Old 04-08-2010
use -R option in your psql -R separator
--record-separator separator Use separator as the record separator. This is equivalent to the \pset recordsep command.

---------- Post updated at 06:33 AM ---------- Previous update was at 06:30 AM ----------

Quote:
Originally Posted by jimmy_y
Hi Everyone,

When i finish running
Code:
[root@tmp]# echo `psql -t -U root databaseA -c "select a, b from book"`;

i get the output
Code:
107275 | 14 107301 | 2 107446 | 6 107820 | 77 107929 | 101

Would like to have the result like:
Code:
107275 | 14
107301 | 2
107446 | 6
107820 | 77
107929 | 101

or can i use printf, sprintf, or awk, etc, to make the output like this? Please advice.

Thanks
Regards,
try like this

Code:
echo `psql -A -R "\r\n"  -t -U root databaseA -c "select a, b from book"`;

# 3  
Old 04-08-2010
Seems cannot, the output is
Code:
107275|14\r\n107301|2\r\n107446|6\r\n107820|77\r\n107929|101

# 4  
Old 04-08-2010
try this one

Code:
echo `psql -F "\r\n"  -t -U root databaseA -c "select a, b from book"`;

Code:
echo `psql -A -F "\r\n"  -t -U root databaseA -c "select a, b from book"`;

# 5  
Old 04-08-2010
Smilie
Quote:
Originally Posted by itkamaraj
try this one

Code:
echo `psql -F "\r\n"  -t -U root databaseA -c "select a, b from book"`;

107275 | 14 107301 | 2 107446 | 6 107820 | 77 107929 | 101

Code:
echo `psql -A -F "\r\n"  -t -U root databaseA -c "select a, b from book"`;

107275\r\n14 107301\r\n2 107446\r\n6 107820\r\n77 107929\r\n101
# 6  
Old 04-08-2010
i see the field seperator is -R option

PostgreSQL: Documentation: Manuals: PostgreSQL 8.3: psql
# 7  
Old 04-09-2010
i give up change the psql line, but below can works.
1. use -A -R "#", then
[code]
107275|14#107301|2#107446|6#107820|77#107929|101
[code]

2. output this output to a string, then $aa =~ s/#/\n/g;
then the output will be
Code:
107275|14
107301|2
107446|6
107820|77
107929|101

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PSQL multiple queries output to file

Hi, I have a shell script containing multiple PSQL queries for which I want the output to be redirected to a text file. psql -U postgres -d database -o textfile.txt << EOF Query1; Query2; Query ....; EOF When executing the script, queries outputs are directed to textfile.txt, however... (2 Replies)
Discussion started by: nms
2 Replies

2. Shell Programming and Scripting

Psql output into array and read 2 fields into different variables

Hello Just edited the entry to make it easier to understand what i want How can i achieve this: GOAL: read 2 field from a table with PSQL result of this PSQL command is this INSTALLEDLANG=$(su - postgres -c "psql -A -t -q -c -d ${DBNAME} -t -c 'SELECT code, iso_code from res_lang'") ... (0 Replies)
Discussion started by: winston6071
0 Replies

3. Shell Programming and Scripting

Get the return value and get the output to $results

cmd() { echo " " echo "$(whoami)@$(hostname):$(pwd)# $*" results=`eval $*` echo $results } I want to get the eval $* 's return value and pass it to a new variable $val, and get "eval $*" 's the ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

4. Shell Programming and Scripting

Return output from shell script

Hi All, There are 2 scripts A and B. A --> It will invoke script B B --> It will generate below output. 100 - connected 105 - Not Connected 210 - Connected I want to return this value to script A. Please advice. (4 Replies)
Discussion started by: Girish19
4 Replies

5. Shell Programming and Scripting

Better to Use Return Code or wc -l Output?

Hey All, Quick question... I'm writing a short script to check if a continuous port is running on a server. I'm using "ps -ef | grep -v grep | grep processName" and I was wondering if it was better/more reliable to just check the return code from the command or if its better to pipe to... (12 Replies)
Discussion started by: mrm5102
12 Replies

6. Programming

Psql replace blanks with character

Well as the title describes, its a pretty straight forward problem. I have a series of psql tables where there are lots of blanks. However there is at least one column, called name, that will never be blank. I want to write a select statement to get all of the contents of the table and then turn... (3 Replies)
Discussion started by: wxornot
3 Replies

7. Windows & DOS: Issues & Discussions

how to return cygwin output to DOS

Hi, I have installed cygwin on my windows XP system. And I am trying to use the following code snippet: set SERVER="grep JDBCConnectionPool config.xml | gawk -F; '{print $2}'" sed -i 's/%SERVER%/WLMDEVDB1/' config.xml But it doesn't work because at runtime %SERVER% gets replaced by "grep... (2 Replies)
Discussion started by: illcar
2 Replies

8. Shell Programming and Scripting

Howto return the single penultimate line from my output

Hi All I have what seems to be something quite trivial but for the life of me can't work out a solution. Basically, I have the following script that reads a version report that contains certain flags. If a condition is true, I want to print the value of column 2, sort them uniquely and return... (2 Replies)
Discussion started by: kingpin2502
2 Replies

9. UNIX and Linux Applications

strange behavior of PSQL user defined function

Segregated the problematic portion, and showing for your view here., 1. Following is the function definition, create or replace function new_del(id integer) returns void as $$ begin raise info 'dollar :%',$1; delete from testing where id=$1; end ; $$ language 'plpgsql'; ... (1 Reply)
Discussion started by: thegeek
1 Replies

10. Shell Programming and Scripting

How to access psql from perl script?

Hi All, How to access the postgreSQL from a perl script? Thanks in advance JS (2 Replies)
Discussion started by: jisha
2 Replies
Login or Register to Ask a Question