Line break in sqlplus output through ksh script

Hi,

I am new to shell script programming. I have written a ksh script to run the sql File placed in server directory and spool the output in destination directory.
Below Command:

$ORACLE_HOME/bin/sqlplus -s $ora_uid @$sqlfile_loc$testquery.sql  > $opfiledirectory

It is generating the output but complete data is in Single Line.
in the .sql file we have below values turned on and off

set feedback off
set echo off
set verify off
set pagesize 0
set heading off
set trim on
set wrap on
set trimspool on
set linesize 9000

current Output Data format:

Item_id|organization_id|ItemName|Item_id|organization_id|ItemName|Item_id|organization_id|ItemName|Item_id|organization_id|ItemName|

Can someone please help how to get output data with each row in each line.
Expected format:

Item_id|organization_id|ItemName|
Item_id|organization_id|ItemName|
Item_id|organization_id|ItemName|
Item_id|organization_id|ItemName|

can we see your sql please?

also how are you viewing the output?

vi? emacs? dumping it to a text file and opening it in notepad?

Below is the SQL:

SELECT &&1 || '|' || to_char(sysdate, 'YYYYMMDD') || 'T' ||
        to_char(sysdate, 'HH24MM') || '00' || '|' || item.segment1 || '|' || 'IM' || '|' ||
        substr(item.description, 1, 50) || '|' || item.primary_UOM_Code || '|' || case
          when item.unit_weight > 0 then
           item.unit_weight
          when item.unit_weight <= 0 then
           9999.9999
          else
           9999.999
        end || '|' || NVL(item.weight_uom_code, 'KG') || '|' || 0 || '|' ||
        'USD' || '|' || 'N' || '|' || '' || '|' || '' || '|' || 'E' || '|' || '' || '|' || '' || '|' ||
        replace(glo_buyer.agent_name, '''', '') || '|' || '' || '|' || case
          when length(hts.tariff_code) < 10 THEN
           '1111111111'
          when hts.tariff_code Is Null THEN
           '1111111111'
          else
           hts.tariff_code
        END || '|' || 'M' || '|' || 'XX' || '|' || 'MULTIPLE' || '|' || '' || '|' || case
          when cst.item_cost > 0 then
           cst.item_cost
          when lacst.item_cost > 0 then
           lacst.item_cost
        --when pendingcst.item_cost > 0 then pendingcst.item_cost
          else
           0
        end || '|' ||
       --cst.item_cost||'|'||
        0 || '|' || 'USD' || '|' || 0 || '|' || 0 || '|' || '' || '|' || 0 || '|' || 0 || '|' || '' || '|' || '' || '|' || '' || '|' || 0 || '|' || '' || '|' || 0 || '|' || '' || '|' ||
        glo_buyer.agent_name || '|' || '' || '|' || ''
  FROM apps.mtl_system_items_b item

(more conditions)

we are generating output in .txt format and viewing output in notepad .

Try a here document like this:
I am assuming the things with a $ are shell variables.
Which you should write like this so somebody later on can figure it out:

# like this: ${variable goes here}
# example:
${ora_uid}

Example here document starts with -EOF, end with EOF:

$ORACLE_HOME/bin/sqlplus -s ${ora_uid} <<-EOF 
[set commands go here]
spool ${opfiledirectory}
@${sqlfile_loc}${testquery.sql}
spool off
EOF
1 Like

On a slightly different note:

when will the "else" branch be executed? :confused:

1 Like

I suspected as much. You have an OS problem not a code problem. Windows and Unix use different end of line characters. You'll need to convert the file in Unix to a windows format before moving it to windows for viewing.

in your Unix box from a shell window do

man unix2dos

This will change the end of line characters to windows versions and allow you to see the line breaks.

1 Like

Thanks everyone for prompt response.
Found that it was issue while copying the file from unix to local system and opening in Notepad. converting the file into text mode while copying solved the issue.
In server files were ok.