CSV file - numbers question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CSV file - numbers question
# 1  
Old 09-21-2010
CSV file - numbers question

Gurus,

I have the following file in csv format, to be convert from cvs to excel.

abc,summer,tennis,123456789,winter
clearsky,fall,computer,43322,monitor

I need to place commas on numbers so that they be read properly in excel.
The number column output I need to see in excel should have for numbers
as follows

abc summer tennis 123,456,789 winter
clearsky fall computer 43,322 monitor

The problem I have is, if I place commas like 123,456,789 in csv file and export it then it would consider each of these digits as a separate column and that would mess up the output.



PS: I am using csv2excel scrip to convert to excel.

Any ideas would be appreciated.
# 2  
Old 09-21-2010
Surely the comma thousands separator is something you change in the column format in the Excel spreadsheet itself not in the input data.
# 3  
Old 09-21-2010
Place the numbers within double quotes. Assuming the numbers are in the fourth field:
Code:
awk -F, '{$4=sprintf("\"%\047.0f\"",$4)}1' OFS="," file > file.csv

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 09-21-2010
Franklin52,

Thanks for your reply. Your commad did work corrects and it placed the commas as expected as below in csv file

abc,summer,tennis,"123,456,789",winter
clearsky,fall,computer,43,322",monitor

But when trying to convert to xls using csv2xls script and then viewing in Excel,
it treated 123 as separate columen, 456 as another column, so also 789.

That brings back me to the same point, how to introduce commas in numbers without excel treating as separate columns.

thanks

---------- Post updated at 01:51 PM ---------- Previous update was at 01:49 PM ----------

Methly,

Thanks for you feedback, after converting to Excel I do not want to format columns manually since this script has to run everyday. I was trying to find a way to convert from csv to excel withou manipulting in excel.

thanks.
# 5  
Old 09-21-2010
Hi.

I can't be very sure (since I don't have access to a Spreadsheet right now), but if you added an equal sign:

Code:
$ awk -F, '{$4=sprintf("=\"%\047.0f\"",$4)}1' OFS="," file

Would that help?
# 6  
Old 09-21-2010
Quote:
Originally Posted by clearcase123
Franklin52,

Thanks for your reply. Your commad did work corrects and it placed the commas as expected as below in csv file

abc,summer,tennis,"123,456,789",winter
clearsky,fall,computer,43,322",monitor

But when trying to convert to xls using csv2xls script and then viewing in Excel,
it treated 123 as separate columen, 456 as another column, so also 789.

That brings back me to the same point, how to introduce commas in numbers without excel treating as separate columns.

thanks
Try another OFS (Output Field Separator).

What is your default field separator of csv files?
# 7  
Old 09-21-2010
another field Separator

Franklin52,

The default field seapartor is comma. This is how I get the raw file from I need to start processing file.

If I need to change the field separator in my raw file .csv how can I do that?

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

2. Shell Programming and Scripting

Save output of updated csv file as csv file itself

Hi, all I want to sort a csv file based on timestamp from oldest to newest and save the output as csv file itself. Here is an example of my csv file. test.csv SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0739.JPG,2015:02:17 11:32:21 /home/intannf/foto/IMG_0749.JPG,2015:02:17 11:37:28... (10 Replies)
Discussion started by: refrain
10 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Perl Question - split function with csv file

Hi all, I have a csv file that appears as follows: ,2013/03/26,2012/12/26,4,1,"2017/09/26,5.75%","2017/09/26,1,2018/09/26,1,2019/09/26,1,2020/09/26,1,2021/09/26,1",,,2012/12/26,now when i use the split function like this: my @f = split/,/; the split function will split the data that is... (2 Replies)
Discussion started by: WongSifu
2 Replies

6. Shell Programming and Scripting

Replace 2nd column of CSV file with numbers on line

I have a csv file with occasional multiple entries in the second column. 111111,104,07-24-2011,3.15,N, 222222,020 140,07-24-2011,10.00,N,I want the result 111111,104,07-24-2011,3.15,N, 222222,020,07-24-2011,10.00,N, 222222,140,07-24-2011,10.00,N, I know I can get the output of the second... (5 Replies)
Discussion started by: ffdstanley
5 Replies

7. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

8. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

9. UNIX for Dummies Questions & Answers

Question About Getting Line Numbers in a File

Ok, this is a unique question. Say I have a word like "jamamamama" in a file called foo.bar. Now, how do you get the line number that the word "jamammama" exist in the file foo.bar without having to go into the foo.bar file to edit it? is there a command i can run on the foo.bar... (4 Replies)
Discussion started by: SkySmart
4 Replies
Login or Register to Ask a Question