Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Not able to write SQL query output in to .csv file with shell script.


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Not able to write SQL query output in to .csv file with shell script.

I am trying to write SQL query output into a .csv file. But in the output columns are displaying in different lines instead of coming in one line.

Main Code shell script:

this is my code:
Code:
#!/bin/bash
file="db_detail.txt"
. $file
rm /batch/corpplan/bin/dan.csv
SPOOL_FILE="/batch/corpplan/bin/dan.csv"
SQL="/batch/corpplan/bin/myquery.sql"
sqlplus -s  UserName/password@database << EOF
set linesize 60
spool on
set head on
set colsep ',';
SPOOL $SPOOL_FILE
@$SQL
SPOOL OFF
EOF

myquery.sql

Code:
SELECT DISTINCT ID_1,USER_NAME,TIME_POSTED FROM Shchemaname.tablename WHERE TIME_POSTED  >  to_date('05/31/2019', 'MM/DD/YYYY') and USER_NAME = 'PL26218';


My Current output is:
Code:
ID_1
------------------------------------------------------------
USER_NAME
------------------------------------------------------------
TIME_POSTED
---------------
1 - Choose COA Seed Options
PL26218
05-JUN-19

Budget Seed On_Off
PL26218
04-JUN-19

ID_1
------------------------------------------------------------
USER_NAME
------------------------------------------------------------
TIME_POSTED
---------------

1 - Choose COA Seed Options
PL26218
04-JUN-19

Desired output is :
Code:
ID_1, USER_NAME, TIME_POSTED
1 - Choose COA Seed Options,PL26218,05-JUN-19
Budget Seed On_Off,PL26218,04-JUN-19
1 - Choose COA Seed Options,PL26218,04-JUN-19

Moderator's Comments:
Mod Comment Please do wrap your samples/codes in to CODE TAGS as per forum rules.

Last edited by RavinderSingh13; 4 Weeks Ago at 11:25 PM..
# 2  
You should attempt to write the code to process (parse) your text.

Your shell script had no basic code to process the text in your output.
# 3  
Those header "underlines" indicate that the first two colums are 60 characters wide and thus won't fit into linesize 60.You can

- trim / chop the columns to sensible lengths
- increase line size so all three fit
- select "one column" only by concatenating the three trimmed column values.

Last edited by RudiC; 4 Weeks Ago at 06:32 AM..
# 4  
Thanks Neo and RudiC for your inputs.

RudiC - i will increase the the line size and try.

Neo - Could you please give me some hint about the part i missed, I am new to unix.
# 5  
Hi Guys,

I am able to get desired output after changing my shell script as mentioned below.

Code:
#!/bin/bash
file="db_detail.txt"
. $file
rm /batch/corpplan/bin/dan.csv
SPOOL_FILE="/batch/corpplan/bin/dan.csv"
SQL="/batch/corpplan/bin/myquery.sql"
sqlplus -s username/password@database << EOF
COLUMN ID_1 HEADING 'FORM_NAME' FORMAT A50
COLUMN USER_NAME FORMAT A50
COLUMN TIME_POSTED FORMAT A50
set linesize 200
spool on
set head on
set colsep '|';
SPOOL $SPOOL_FILE
@$SQL
SPOOL OFF
EOF

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments (as required by forum rules).

Last edited by Don Cragun; 4 Weeks Ago at 03:26 AM..
This User Gave Thanks to sandeepgoli53 For This Post:
# 6  
Thanks for sharing your final code. Glad you found a solution. Pls be aware that this is NOT coming close to the desired output in post #1.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Shell script appending output of sql query
itzkashi
I am writing the following script to create the file v_out.txt. sqlplus -s /nolog << EOF CONNECT scott/tiger@orcl; whenever sqlerror exit sql.sqlcode; set newpage 0; SET PAGESIZE 0; SET ECHO OFF; SET FEEDBACK OFF; SET HEADING OFF; SET VERIFY OFF; SET LINESIZE 100; set tab off; set...... Shell Programming and Scripting
7
Shell Programming and Scripting
Run sql query in shell script and output data save as delimited text
Jaganjag
I want to run sql query in shell script and output data save as delimited text (delimited text would be comma) Code: SPOOL_FILE=/pgedw/dan.txt SQL=/pgedw/dan.sql sqlplus -s username/password@myhost:port/servicename <<EOF set head on set COLSEP , set linesize 32767 SET TRIMSPOOL ON SET...... Shell Programming and Scripting
8
Shell Programming and Scripting
SQL Query in Shell Script output formatting
Kevin Tivoli
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...... Shell Programming and Scripting
2
Shell Programming and Scripting
Problem in formatting output of SQL query in excel sheet in shell script
Agupte
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...... Shell Programming and Scripting
3
Shell Programming and Scripting
How to use sql data file in unix csv file as input to an sql query from shell
Nareshp
Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s...... Shell Programming and Scripting
2
Shell Programming and Scripting

Featured Tech Videos