LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   writting a csv file (https://www.linuxquestions.org/questions/linux-newbie-8/writting-a-csv-file-704047/)

ajinkyakp 02-11-2009 10:54 PM

writting a csv file
 
I am a newbie to linux
Can anyone please tell me how to write a csv file ?

anomie 02-11-2009 11:11 PM

AFAIK, a csv file is just an ascii text file that contains fields separated by commas.

Code:

[fugu ~]$ for _i in $(seq 1 5) ; do echo "foo${_i},bar${_i},baz${_i}" >> somefile ; done
[fugu ~]$ cat somefile
foo1,bar1,baz1
foo2,bar2,baz2
foo3,bar3,baz3
foo4,bar4,baz4
foo5,bar5,baz5


JulianTosh 02-11-2009 11:13 PM

csv = comma separated values


examples:
field1,field2,field3
"First Name","Last Name","Address"

Simple as that..

jschiwal 02-12-2009 01:27 AM

For number fields, commas separate the fields. For text fields, usually the contents of the field is contained in double quotes. What is the source? Are you converting one file type to a csv file (a filter) or is this the output you want for your application?

You could take a spreadsheet, and export a table as a CSV file, and then examine it. There can be problems if your text field can contain a double quote or a comma. The double quotes are used in text fields because for fields like addresses, commas are common.

jdkaye 02-12-2009 03:26 AM

Quote:

Originally Posted by jschiwal (Post 3440611)
You could take a spreadsheet, and export a table as a CSV file, and then examine it. There can be problems if your text field can contain a double quote or a comma. The double quotes are used in text fields because for fields like addresses, commas are common.

Openoffice scalc (the spreadsheet) deals with that issue pretty well. You can indicate what your separator is (comma, tab, semicolon, hash, etc.) and what your field delimiter is (if any). That shouldn't be much of a problem then.
Cheers,
jdk

salasi 02-13-2009 11:01 AM

Err, you use write/file save/save as. If you are using some particular application, it is whatever that application uses to write files, but you have to tell the application that you want a csv file, which varies a bit depending on the application.

If you are using an application that doesn't support writing csv files, then that would be a problem, but you may still be able to do it, either going via an intermediate app that does write csv files, or writing a little bash/perl/python/sed/awk script that converts something that you can write (and understand) into what you want.

bach-fiend 02-16-2009 11:29 AM

CSV starts simple but can get trickier, for example, if you have a quoted string with other quote marks in it. If you are familiar with a scripting language like Perl, I'd suggest you use a well established CPAN module to write the file. I imagine other tools like python also have prewritten (and preDEBUGGED) library tools.

Another approach is to use files where the fields are delimited by TAB characters. That's what I did when I used Microsoft Access. (I think Microsoft let you specify the delimiter in case fields of your file can contain tabs. Another thing to watch out for: some editors immediately convert tabs to the "appropriate number" of spaces, but you may be able to turn that "feature" off.


All times are GMT -5. The time now is 06:34 AM.