csv to xls : missing rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting csv to xls : missing rows
# 1  
Old 01-23-2012
csv to xls : missing rows

A unix script generates a file "1.csv". I use the following to email this as an excel sheet.


HTML Code:
/usr/bin/uuencode /tmp/1.csv 1.csv > $PATH/attachment.txt
mailx -r abc@domain.com -s "Subject" myself@domain.com < $PATH/attachment.txt
I get the file as CSV in the email and everything is fine except the following missing rows problem:

PROBLEM: Some fields in the CSV have a quotemarks ( " ) because of which some records are missing in the CSV in the email, though these rows are present on the csv file on the unix box.
Also, if the csv email attachment is opened in a notepad, those rows can be seen.

Is there a way of getting those missing rows in the excel, please?

Regards,
Girish.
# 2  
Old 01-23-2012
csv is csv, you didn't actually convert it to xls before mailing it. excel simply understands how to read csv.

Show an example of the data that didn't work.
# 3  
Old 01-23-2012
Hi Corona,

Here is an example:

Code:
1,Jack,a,cdef,23,w1e2r3t4,"Martin             
.
.
30,Howard,a,bjoa,28,1m0df5s9,Joel             
.
.

43,Sunil,a,xhst,45,x4z3v8c9,"Girish

Notice that the last column of the 1st and the last row have a single quote mark. I think because of which all the rows between these two rows are missing in the email attachment when it is opened as an excel sheet. however, they are seen if opened in a notepad.
# 4  
Old 01-23-2012
Would stripping out quotation marks be sufficient, or do they have to be preserved?

Code:
tr -d "\"'" < input.csv > /tmp/$$.csv
# mail the $$.csv

Or if a backslash before the quote will prevent excel from swallowing them:

Code:
sed 's/"/\\"/g' < input.csv > /tmp/$$.csv


Last edited by Corona688; 01-23-2012 at 02:17 PM.. Reason: typo
# 5  
Old 01-23-2012
Thanks for the suggestions but I tried this already and it does work. But unfortunately I need to keep the quote mark as it is ! Smilie
# 6  
Old 01-23-2012
Maybe it only understands backslashes inside quotes...

Code:
awk -v FS="," -v OFS="," '{ gsub(/"/, "\\\""); for(N=1; N<=NF; N++) $N="\"" $N "\"" } 1' file.csv > /tmp/$$.csv

which will turn the field a into "a", and the field "a into "\"a".
# 7  
Old 01-23-2012
Code:
sed 's/\"/\\\"/g' a.csv >> a_tmp.csv
mv a_tmp.csv a.csv



Using the above

aaa,aaa,aaaaaa,aaaaaaaaa,aaaaaaaaaaa,"aaaaaaaaaaa (in CSV)

is seen as the following in notepad


aaa,aaa,aaaaaa,aaaaaaaaa,aaaaaaaaaaa,\"aaaaaaaaaaa (in CSV)


And in the excel sheet also it looks like the following:

aaa,aaa,aaaaaa,aaaaaaaaa,aaaaaaaaaaa,\"aaaaaaaaaaa (in excel)

But I want the following in excel sheet:

aaa,aaa,aaaaaa,aaaaaaaaa,aaaaaaaaaaa,"aaaaaaaaaaa (in excel)


The problem is that it seems a property of excel to put everything between 2 double quotes in one field or if a field value bigins with a single double quote, it automatically ends that field with a double quote as well or puts all rows after it into one field.

I think I might have found the solution though. If so, I will post it shortly. thanks again for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Csv to xls

Hello I have a script which converts log to csv. Now I need to have xls. Is there any easy way/command which can convert csv to xls?:confused: preferably just using bash and not perl,... is it possible? (1 Reply)
Discussion started by: frhling
1 Replies

2. Shell Programming and Scripting

Oracle to CSV to XLS

I would like to know if have one way with read table from oracle converter in CSV o TXT and After converter in XLS or spreedsheet Thanks so much JAvier (3 Replies)
Discussion started by: javeiregh
3 Replies

3. Shell Programming and Scripting

CSV to XLS

Ok, every morning at my office we send out excel sheets to Economy people with statistics for yesterdays trading. All the trading run's in Redhat or Solaris environments. We run a script on a Redhat server whitch generates the stats in CSV format. After we download we open it in Excel and... (3 Replies)
Discussion started by: chipmunken
3 Replies

4. Shell Programming and Scripting

how to convert .xls to .csv

Hi, I have problem..How to convert .xls file to .csv.. Plz help me for this problem.. (1 Reply)
Discussion started by: varma457
1 Replies

5. Shell Programming and Scripting

Shell convert xls to csv

Hi does anybody know how to convert xls to csv undex linux. I need only data (that is log from test, dont need any macro and so on) from xls. Any idea how to do that? Perl? shell? Could you give me any example? Thanks in advance for answer. Gracjan (4 Replies)
Discussion started by: Gracjan
4 Replies

6. Shell Programming and Scripting

converting xls file to txt file and xls to csv

I need to convert an excel file into a text file and an excel file into a CSV file.. any code to do that is appreciated thanks (6 Replies)
Discussion started by: bandar007
6 Replies

7. Shell Programming and Scripting

xls to csv

how to convert a xls file into .csv file? is tghere any command in unix for that? please help thanks (3 Replies)
Discussion started by: infyanurag
3 Replies

8. Shell Programming and Scripting

.xls to .csv conversion

Hi Please can someone tell me how i can convert .xls file into .csv on both platforms, windows and unix. many thanks, neil (4 Replies)
Discussion started by: neil546
4 Replies

9. Shell Programming and Scripting

From xls to csv file

Can we convert an xls file into csv format in Unix Thanks Suresh (1 Reply)
Discussion started by: sureshg_sampat
1 Replies

10. Shell Programming and Scripting

Converting csv to xls

Hi, Can anyone tell the option to change the file type in unix. i.e. if a file is in csv(Comma Separating Values) format, it should be changed to xls(ordinary MS-Excel) format. But renaming command is not changing to correct file format. Thanks in advance, Milton. (1 Reply)
Discussion started by: miltony
1 Replies
Login or Register to Ask a Question