简体   繁体   English

使用BASH脚本假脱机CSV文件| 生成的CSV中的输出错误

[英]Spool CSV file using BASH Script | Errors with Output in generated CSV

I need to spool the output of tables from db to CSV file. 我需要将表的输出从db假脱机到CSV文件。 I checked and referred to How do I spool to a CSV formatted file using SQLPLUS? 我检查并参考了如何使用SQLPLUS后台处理到CSV格式的文件?

But it's not working successfully. 但是它不能成功工作。 What I am getting is only the first 3 columns being separated by , and not the remaining. 我得到的只是前3列用分隔,而不是其余。 Others are getting separated by newline. 其他人则被换行符隔开。

EDIT : Schema Details of Table being Spooled. 编辑:后台处理的表的架构详细信息。

WWID               VARCHAR2 (5 Byte) 
TIMELOG_DATE       DATE 
TOTAL_HOURS        NUMBER (10,2) 
ACTIVITY           VARCHAR2 (100 Byte) 
SUBACTIVITY        VARCHAR2 (100 Byte) 
REF_PROJECT_ID     VARCHAR2 (30 Byte) 
REF_PROJECT_DESC   VARCHAR2 (250 Byte)
WORK_REQUEST_ID    VARCHAR2 (30 Byte)
EMP_COMMENTS       VARCHAR2 (100 Byte)
APPROVER_COMMENTS  VARCHAR2 (100 Byte)

Script: 脚本:

echo "\n>>> ******Data Processing Started at `date '+%d-%m-%Y %T %Z'`******" >>${LOGFILE}

sqlplus $fims_user/$fims_pwd << EOF
set serveroutput on;
set colsep ,     ;-- separate columns with a comma
set pagesize 0   ;-- No header rows
set trimspool on ;-- remove trailing blanks
set headsep off  ;-- this may be useful...depends on your headings.

spool /home/fimsctl/datafiles/outbound/timelog/timelog_file_`date +%y%m%d`.csv

select * from FIMS_OWNER.TIMELOG_EXTRACT_OUTBOUND_T;

commit;
spool off
exit
EOF

echo "\n>>> ******Data Load Completed at `date '+%d-%m-%Y %T %Z'`******" >>${LOGFILE}

echo "End of the script">> ${LOGFILE}

And Output in CSV i am getting is: 我得到的CSV输出是:

SQL> select * from FIMS_OWNER.TIMELOG_EXTRACT_OUTBOUND_T;
iv315,29-DEC-14,          8
DUMMY

REF01
New Ref Project of type CPRM
66
NA


iv315,30-DEC-14,          8
DUMMY

REF01
New Ref Project of type CPRM
66
NA


iv315,31-DEC-14,          8
DUMMY

REF01
New Ref Project of type CPRM
66
NA

That is values are then separated by newline(when seen in wordpad) 那就是值然后由换行符分隔(当在写字板中看到时)

Thanks 谢谢

The defaut linesize is 80 characters, so by default columns will wrap onto new lines when that length will be exceeded. defaut的行大小为80个字符,因此默认情况下,超过该长度时,列将换行。 Your 100-byte columns will cause that behaviour. 您的100个字节的列将导致该行为。 You can add SQL*Plus commands to change that: 您可以添加SQL * Plus命令来更改它:

-- any number at least as large as the longest possible output
set linesize 1024
set wrap off

select * is generally frowned up as the output can change unexpectedly if the table definition changes - ie a column is added - or if the table has columns in different orders in different environments. select *通常会皱眉,因为如果表定义更改(即添加了列)或表在不同环境中具有不同顺序的列,则输出可能会意外更改。 (Which arguably shouldn't happen with source control, and generally doesn't matter as long as you don't use * . If you list the columns you want explicitly, it isn't much extra work to concatenate them with manually-added separators, eg: (可以说这不应该在源代码控制中发生,并且通常只要您不使用*就没有关系。如果您显式列出想要的列,那么用手动添加的方式将它们连接起来并不需要太多的工作。分隔符,例如:

select wwid
  ||','|| to_char(timelog_date, 'DD-MON-YY') -- or another format
  ||','|| total_hours
  ||','|| activity
  ||','|| subactivity
  ... -- etc
from FIMS_OWNER.TIMELOG_EXTRACT_OUTBOUND_T;

That will remove extra whitespace that is currently going to pad all the CSV rows to the same length, and reduce the output file size. 这将删除当前将所有CSV行填充为相同长度的多余空格,并减小输出文件的大小。 The output is then a single column, so the colsep setting isn't relevant any more. 然后,输出为单列,因此colsep设置不再相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM