LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2009, 11:54 PM   #1
ajinkyakp
LQ Newbie
 
Registered: Feb 2009
Posts: 1

Rep: Reputation: 0
writting a csv file


I am a newbie to linux
Can anyone please tell me how to write a csv file ?
 
Old 02-12-2009, 12:11 AM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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
 
Old 02-12-2009, 12:13 AM   #3
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
csv = comma separated values


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

Simple as that..
 
Old 02-12-2009, 02:27 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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.
 
Old 02-12-2009, 04:26 AM   #5
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,464

Rep: Reputation: Disabled
Quote:
Originally Posted by jschiwal View Post
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
 
Old 02-13-2009, 12:01 PM   #6
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,059

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
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.
 
Old 02-16-2009, 12:29 PM   #7
bach-fiend
LQ Newbie
 
Registered: Feb 2009
Location: St Paul, Minnesota, USA
Distribution: Redhat Fedora 10; someday planet CCRMA
Posts: 11

Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 10:15 PM
Why a file writting command resulting an empty file? marhen Programming 9 05-05-2008 04:49 AM
Reading text file-writting binary file cdog Programming 5 06-13-2006 12:56 PM
Is a file in writting ? stephnane AIX 3 08-03-2004 12:17 AM
Is a file in writting stephnane Linux - General 2 07-30-2004 10:39 PM


All times are GMT -5. The time now is 05:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration