remove blank lines from csv using shell script for oracle - oracle

Hi am using following shell script to create csv from oracle database
export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export PATH=$PATH:$ORACLE_HOME/bin
sqlplus -s user1/user1<<!
Spool on
set heading off
SET FEEDBACK OFF
set wrap off
set colsep ,
SET LINESIZE 200
set trims on
Spool /home/ist/username.csv
select 'username','userid' from dual;
select username,userid from users;
Spool off
EXIT
!
I am getting following output
as you can see there is blank line at first and third row
but am expecting file without blank lines.
Any help will be appreciated.

Use the
SET PAGESIZE 0
command to avoid the blank lines. this also suppresses column headings, so you can remove
SET HEADING OFF
The command
SPOOL on
does not make sense because it starts spooling in a file named on.lst. So remove this command, too.
If you want to display the heading with the column name
you can try the following settings
set HEADING ON
SET FEEDBACK OFF
set WRAP OFF
set COLSEP ,
SET LINESIZE 32767
set NEWPAGE none
set UNDERLINE OFF
set TRIMSPOOL ON
set TRIMOUT ON
set PAGESIZE 50000
´heading on´ is the default so you must not set it. It enables the display of the column names when a select starts. underline off suppresses the '---' line between column names and data of a select. pages 50000 sets the pagesize to its maximum value (Oracle 11.2). linesize 32767 sets the linesize to its maximum value (Oracle 11.2). newpage none is necessary to suppress this empty line at the beginning of a page that was the primary concern of your posting.
All this can be found in the SQL*Plus Command Reference
The termout off parameter suppresses only output created by a scripts that is executed with the # or ## command. It dos not suppress out by a command entered in the SQL*plus console. If you use
sqlplus user/passw#connect <<!
...
!
you use the here-document syntax of the shell language which simulates the interactive input. So put your sql commands in a script, e.g. script.sql, and execute
sqlplus user/passw#connect #script.sql
then termout off will suppress terminal output.
Instead of
colsep ,
select username,userid
...
which returns something like
user1 , 14
nextuser , 236
myuser , 11
...
you can use leave the COLSEP unchanged and execute
select username||','||userid
...
to get the following output
user1,14
nextuser,236
myuser,11
...
Maybe this is useful
https://dba.stackexchange.com/a/64620/2047

set trimspool on
this will work for you

put this on your script
sed -i '/^\s*$/d'/home/ist/username.csv

Remove lines in /home/ist/username.csv without a , using
grep "," /home/ist/username.csv > /home/ist/username2.csv
# or
sed -i '/^[^,]*$/ d' /home/ist/username.csv

SQLPLUS 12.2 has an feature to output CSV, but you need to configure some settings to remove blank lines on it. Here is my approach
SET HEADING OFF;
SET PAGES 0;
SET SQLBLANKLINES ON;
set echo off;
set autotrace off;
set feedback off;
SET TRIM ON;
SET MARKUP CSV ON DELIMITER | QUOTE OFF;

Related

Export SQLPluse Query from Oracle to CSV with UTF-8 encoding

I am using the below SQLPluse Script to export data out of Oracle into CSV files. The issue is I need the files to be in UTF-8 and its currently ANSI. How do i go about getting the files to be UTF-8?
SET colsep ';'
SET TERMOUT OFF
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 800
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
SET NUMW 50
column timecol new_value timestamp
column spool_extension new_value suffix
SELECT '.csv' spool_extension
FROM dual;
/
SPOOL C:\job_files\production\Data_Upload\TEST_1&&suffix
##"C:\job_files\production\Data_Upload\TEST_1";
/
Exit;
Not entirely clear from your question what you mean, but I assume the issue is the data your query returns contains UTF8 data but your output does not show the UTF8 characters correctly.
The NLS_LANG environment variable controls how Oracle tools/programs including SQL*Plus handle such data.
You would normally set the variable to something that matches the database, e.g
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
Then run SQL*Plus.
You can query NLS_DATABASE_PARAMETERS to see what language settings are in the database and experiment with the NLS_LANG variable as above.
NLS_LANG value format is LANGUAGE_COUNTRY.CHARSET.

Remove/Prevent First Blank from Oracle CSV MARKUP output file

I am currently writing scripts to output table data to CSV files so that they can be ingested into another platform. I'd much rather use the set markup CSV on quote on; SQL option to generate my output file. as it makes the script much cleaner and a whole lot simpler to write.
The problem, however, is that no matter how hard I try, I cannot seem to find an option that will prevent the output file from starting with a blank line. I need the column headers in the very first line and not in the second line.
The scripts need to run on a Windows Server against an Oracle 12c (12.1.0.2.0) Database, using SQLPLUS to execute the SQL commands and PowerShell/batch as the scripting wrapper.
After searching for some time and trying various options that only seem to have any effect on older approaches to CSV outputs. Eventually, I found the Oracle documentation for the SET command and discovered that things like SET NEWPAGE, SET TRIMSPOOL have no effect when using SET MARK[UP].
Oracle SET Command documentation is here: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqpug/SET-system-variable-summary.html#GUID-0AA910C4-C22A-4A9E-BE13-AAA059CC7919
Is there a simple way to get rid of or suppress the first line that is blank? I really don't want to have to write further file processing scripts that go and remove the line from the file using PowerShell/Batch. That just seems like a really dumb thing to have to do...
A simplified version of my SQL is below and the output still includes the blank first line.
column thespoolfile new_value thespoolfile
select 'D:\Temp\outfile-detail-' || TO_CHAR( sysdate, 'YYYYMMDDHH24MI' ) || '.csv' as thespoolfile from dual;
set markup csv on quote on;
SET FEEDBACK OFF;
set trimspool on;
SET HEADSEP off;
SET SQLBLANKLINES off;
SET termout OFF;
spool &thespoolfile;
set define off;
select *
from <table>;
spool off;
exit
You need to use the following option
SET NEWPAGE NONE
It will suppress the first blank line, which is bothering you.
Cheers!!

How to spool file1 or file2 from a single SQL file based on the input?

I have a SQL file like the following:
SET ECHO OFF
set feed off
set verify off
set head off
set pagesize 0
set space 0
set trimspool on
set line 250
spool subir.rpt
select 'Subir' from dual;
spool off
SET ECHO OFF
set feed off
set verify off
set head off
set pagesize 0
set space 0
set trimspool on
set line 250
spool vishal.rpt
select 'vishal' from dual;
spool off
Let's call this file spool.sql. I need to enable creation of spool file based on the input spool.sql file receives. For example, if it receives "subir.rpt" The first portion of the SQL file should run and subir.rpt should be produced; if it is vishal.rpt, the second portion should run and vishal.rpt should get generated.
How can this be achieved?
Edit: This SQL file is getting called from a shell script. The shellscript is like the following:
sqlplus -s dbread$HOST_CONNECT_STR/dbread<<endplus
#spool.sql
endplus
What is launching this spool.sql? A shell script? Database function?
If it's a shell script, I would either use a parameter to this script or split it into two separate .sql files and call the appropriate one.
You could also save the file value into a temporary table prior to calling this script and select it before spooling the file.
There are a lot of options but it all depends on how you call this script and how extensible you want it to be.
The simplest way would be to use the supplied parameter dynamically as the spoolfile:
set echo off
set feed off
set verify off
set head off
set pagesize 0
set space 0
set trimspool on
set line 250
spool &1
spool
select 'Subir' from dual where '&1' = 'Subir.rpt';
select 'vishal' from dual where '&1' = 'vishal.rpt';
spool off
(The spool command on its own is just to print the current spoolfile name for demo purposes.)

Sqlplus printing the result twice and with an empty line

I write shell script and want to use sqlplus, when I write:
#!/bin/bash
result=$(sqlplus -s user/pass#DB << EOF
set trimspool on;
set linesize 32000;
SET SPACE 0;
SELECT MAX(DNNCMNT_ANSWER_TIME) FROM TKIMEI.DNNCMNT_IMEI_APPRV;
/
exit;
EOF)
echo "$result"
the result is in txt file (I'm executing it as ksh sql.sh > result.txt):
MAX(DNNCM
---------
10-MAR-14
MAX(DNNCM
---------
10-MAR-14
it is automatically putting an empty line at the beginning of file and writing the result twice.
How can I fix it ?
Remove the slash. It's causing the previous command (the select) to be repeated:
http://docs.oracle.com/cd/B10501_01/server.920/a90842/ch13.htm#1006932
Also, talk to your DBA about setting up external OS authentication so you don't have to hardcode the password in a shell script for security reasons. Once set up, you can replace the login/password combo with just a slash:
http://docs.oracle.com/cd/E25054_01/network.1111/e16543/authentication.htm#i1007520

Export to a csv file

I try many times to export my result into .csv file but I'm always fail. I read lot of articles related in my problem.
Look at my query:
spool sample.csv
SELECT /*html*/(CODE_SALESROOM) POS_ID, (NAME_SALESROOM) POS_NAME
FROM OWNER_DWH.DC_SALESROOM
WHERE CODE_SALESROOM NOT IN ('XAP', 'XNA', '10001');
spool off;
The output of this, when I export in a csv file is only the query not the real result.
What's wrong in my command to export the result?
Please help me. Thanks
Make sure that your query returns values. You can set various parameters of Sql plus before spooling to get desired out put. Below is the list that I use before spooling. And since your are generating csv, it should be comma separated
set echo off
set termout off
set define off
set heading off
set pagesize 10000
set linesize 500
set feedback off
set verify off
set trimspool on
spool sample.csv
SELECT 'POS_ID,POS_NAME' from dual; -- header list
SELECT CODE_SALESROOM || ',' || NAME_SALESROOM
FROM OWNER_DWH.DC_SALESROOM
WHERE CODE_SALESROOM NOT IN ('XAP', 'XNA', '10001');
spool off;
You can modify the set parameters based on your requirement.

Resources