Redirect Postgresql Select Query to Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirect Postgresql Select Query to Variable
# 1  
Old 12-05-2008
Redirect Postgresql Select Query to Variable

How to redirect postgresql select query to a variable using shell scripts?

I tried to use

psql -c "select ip from servers" db | egrep '[0-9]+\.[0-9]+\[0-9]+\[0-9]+' | sed 's/^ *\(.*\) *$/\1/'

Output:
10.10.10.180
10.10.10.181

It display ip addresses from the databasem, but how could i redirect the result to an array/how to loop the ip addresses?

Thanks in advance.

Last edited by uativan; 12-05-2008 at 10:57 PM..
# 2  
Old 12-06-2008
Hi,

declare -a array
array=( $(psql -c "select ip from servers" db | egrep '[0-9]+\.[0-9]+\[0-9]+\[0-9]+' | sed 's/^ *\(.*\) *$/\1/') )

You can access the items in the array with:

Code:
echo ${array[1]}; echo ${array[2]}; ...

or loop through the array:

Code:
for i in ${array[@]}; do echo $i; done

Note: You don't need grep. Sed could do this, too.

HTH Chris
# 3  
Old 12-07-2008
Quote:
Originally Posted by Christoph Spohr
Hi,

declare -a array
array=( $(psql -c "select ip from servers" db | egrep '[0-9]+\.[0-9]+\[0-9]+\[0-9]+' | sed 's/^ *\(.*\) *$/\1/') )

You can access the items in the array with:

Code:
echo ${array[1]}; echo ${array[2]}; ...

or loop through the array:

Code:
for i in ${array[@]}; do echo $i; done

Note: You don't need grep. Sed could do this, too.

HTH Chris
Thanks Chris

Could you tell me how to use sed only?

I tried the sed command but still not success.
# 4  
Old 12-08-2008
You can combine search and replacement in sed. Syntax:

Code:
sed -n '/pattern/s/foo/bar/p' file

sed -n -- only print lines when asked to do so
'/pattern/ -- search for pattern
s/foo/bar/ -- if found replace only on this line foo by bar
p -- print lines only if substitution took place.

HTH Chris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass Variable from shell script to select query for SqlPlus?

echo "set echo off"; echo "set feedback off"; echo "set linesize 4000"; echo " set pagesize 0"; echo " set sqlprompt ''"; echo " set trimspool on"; Select statement is mentioned below echo "select res.ti_book_no from disney_ticket_history res where res.ti_status =${STATUS} and... (7 Replies)
Discussion started by: aroragaurav.84
7 Replies

2. UNIX for Advanced & Expert Users

mysql select query optimization..

hi.. i need to optimize my select query .. my situation is like this .. i have 15 lac recors in my table.. the following query takes nine seconds to give the required output.. SELECT max(ah.AUC_AMT), SUBSTRING_INDEX(GROUP_CONCAT(SUBSTRING_INDEX(ah.AUC_CUS_NAME,'@',1) order by AUC_AMT... (1 Reply)
Discussion started by: senkerth
1 Replies

3. Shell Programming and Scripting

mysql select query optimization..

hi.. i need to optimize my select query .. my situation is like this .. i have 15 lac recors in my table.. the following query takes nine seconds to give the required output.. SELECT max(ah.AUC_AMT), SUBSTRING_INDEX(GROUP_CONCAT(SUBSTRING_INDEX(ah.AUC_CUS_NAME,'@',1) order by AUC_AMT... (0 Replies)
Discussion started by: senkerth
0 Replies

4. Shell Programming and Scripting

Select query implement in a shell

I have been asked to create a shell script that accepts a number of SQL select queries as input, runs them in sequence, spools the output to an EXCEL workbook, where, each worksheet is an output of a Select statement run above. The workbook should be in a .XLS format. If a particular select... (2 Replies)
Discussion started by: ShellNovice1
2 Replies

5. UNIX for Dummies Questions & Answers

how to - redirect query results to a variable

How can I send the results of a query to a unix variable. I basically want to run a query then do some logic on the results. Trying to redirect the result into a variable I define in the script. select count(*) as counter from table - nut to redirect the "count" returned from the query... (2 Replies)
Discussion started by: rstone
2 Replies

6. Shell Programming and Scripting

In a csh script, can I set a variable to the result of an SQLPLUS select query?

Can someone tell me why I'm getting error when I try to run this? #!/bin/csh -f source ~/.cshrc # set SQLPLUS = ${ORACLE_HOME}/bin/sqlplus # set count=`$SQLPLUS -s ${DB_LOGIN} << END select count(1) from put_groups where group_name='PC' and description='EOD_EVENT' and serial_number=1;... (7 Replies)
Discussion started by: gregrobinsonhd
7 Replies

7. Shell Programming and Scripting

How to redirect value from sql select statment to unix variable

Hi, I need to get the value from the table using the sql command and store this value into the unix variable so that i can use this value for furthure use.. Please can any body help me in this regards Thanks & Regards Abdul Hafeez Shaik (17 Replies)
Discussion started by: abdulhafeez
17 Replies

8. UNIX for Dummies Questions & Answers

Redirect Query o/p to variable

Hi, I wanted to o/p the number of rows in a table to a variable in linux. How can i achieve this. I wrote the query and its settings like feedback, pagesize line size in a file and using this file as a parameter to the sqlplus command. now can i redirect the o/p of that query to a variable.... (2 Replies)
Discussion started by: Swapna173
2 Replies

9. UNIX and Linux Applications

Perl - PostGreSql query

Guys, I guess, I posted something in the wrong forum. Here it is - https://www.unix.com/shell-programming-scripting/67395-perl-postgrepsql-question.html Can you please help me with this? Regards, garric (0 Replies)
Discussion started by: garric
0 Replies

10. Shell Programming and Scripting

Need Help (Select query)

Dear All, This may sound a simple query but a non technical person like me is not able to do it, So please help me out. I m using Unix... isql I jst wanted to do something like following select * from xyz where ID= xxxxxxxx (8 digit ID) Here if i put single 8 digit ID then the... (5 Replies)
Discussion started by: topgear1000cc
5 Replies
Login or Register to Ask a Question