formatting into CSV format of SQL session output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting formatting into CSV format of SQL session output
# 1  
Old 10-28-2010
Error formatting into CSV format of SQL session output

I am getting a no of fields from a SQL session (e.g. select a,b,c from table). How do I convert the output values into CSV format .
The output should be like this 'a','b','c',
# 2  
Old 10-28-2010
If the below one helps...!
Code:
echo "a,b,c" |sed "s/\(.\),*/'\1',/g"

# 3  
Old 10-28-2010
Java Its not the desired output :

Suppose the select query from the sql session is like that :
Code:
 
SQL > select a, b,c from table;

Now, if I am assigning this to a variable named VAR, then it will be like this :

VAR = a b c

My question was :

How do I for mat the above value into following :
'a','b','c',
# 4  
Old 10-28-2010
If this is your input VAR="a b c" (there should be no space surrounding = when assigning to variables)then
Code:
echo $VAR| sed "s/\(.\) */'\1',/g" # note there is a space before *

# 5  
Old 10-28-2010
If it is oracle then you can use concatenation operator ||

Code:
select ''''||a||''',' , ''''||b||''',' ''''||c||''',' from table;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

2. Shell Programming and Scripting

SQL Query in Shell Script output formatting

Hi All, #!/bin/ksh call_sql () { sql=$1 sqlplus -s $sqlparam_sieb <<EOF SET ECHO OFF; SET NEWPAGE NONE; SET SQLBL OFF; SET VERIFY OFF; SET LINESIZE 2000; SET... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

3. Shell Programming and Scripting

Problem in formatting output of SQL query in excel sheet in shell script

Hi Guys.. Need your help to format the output of my shell script. I am using spool command to take out put in csv file. below is my code. (for example) col USERNAME for a15 col EMAIL for a30 col FULL_NAME for a20 col LAST_LOGIN for a40 col DATE_CREATED for a40 SPOOL 120.csv... (3 Replies)
Discussion started by: Agupte
3 Replies

4. Shell Programming and Scripting

sql select command output formatting in shell script

Hi, I need to connect to the database and retrieve two variables from the database and store them in a variable,out of these two variables I need to get lastdigit appended to the variable 1 retrieved and variable 2 with out any modification in short select var,data from usage; o/p=... (1 Reply)
Discussion started by: rkrish
1 Replies

5. Shell Programming and Scripting

Read a CSV file and generate SQL output

Friends, This is what I need: I will pass a CSV file as an input, and I want my shell to be reading that CSV file, and based on the parameters it should generate SQLs and write those SQL in a different file in the same location. I'm new to Shell scripting. I'm currently working on a... (25 Replies)
Discussion started by: Ram.Math
25 Replies

6. Shell Programming and Scripting

to read a CSV file and generate SQL output

Friends, This is what I need: I will pass a CSV file as an input, and I want my shell to be reading that CSV file, and based on the parameters it should generate SQLs and write those SQL in a different file in the same location. I'm new to Shell scripting. I'm currently working on a... (1 Reply)
Discussion started by: Ram.Math
1 Replies

7. Shell Programming and Scripting

Output from sql session having an extra whitespace

I am running a sql session within a shell script. Later I am performing some validations on the value coming from the sql session. The problem is that, the value I am getting from sql session have an extra white space at the begining(I am actually, assigning the outcome of the sql session to a... (5 Replies)
Discussion started by: mady135
5 Replies

8. Shell Programming and Scripting

SQL Script's output to a CSV file

I need to call and execute an SQL script within a KSH script and get the output/extracted data into a CSV file. Is there any way to get the out put in a CSV file other than spooling ? I tried spooling. Problem is if there is any wrning/comment/Error they all will be spooled into the csv file. I... (4 Replies)
Discussion started by: Sriranga
4 Replies

9. Shell Programming and Scripting

Formatting isql output to horizontal format

Hi I am formatting informix isql output(vertical) to horizontal format. Suppose I have the following content in the flat file from isql output - item_nbr 0 usfn_label Subscriber Class usfn_name SBCLASS usfn_value bl5 item_nbr 1 usfn_label Switch Name usfn_name switchName... (2 Replies)
Discussion started by: nsinha
2 Replies

10. UNIX for Dummies Questions & Answers

Option in sql script to include column headers when spooling file to .csv format

Can anyone help me how to include COLUMN HEADER when spooling file to .CSV format through SQL statement. Thanks, Akbar (4 Replies)
Discussion started by: s1a2m3
4 Replies
Login or Register to Ask a Question