By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

TEXT Files to CSV

P: 19
Hi Freinds ,

I want to convert the files that are in text format (.txt) to CSV file. I am working on Linux. So can anyone guide me.

Here is my problem Description
I have some result files which contains text fields and numeric fields which are seperated by tab and also there is a - between two numbers 10-20 .

Another thing is that colons are also present in between in files
name :: praveen

Also my another concern is if have different kinds of output fields in the file how to convert those into csv. what i mean is , for example if there is a text file in which i will have the following fields

Name Age Location School College

So in the result files all the above columns may be present or any of these may be present . But minimum first 3 columns will be present . So what is the solution . Does the csv file contain a blank if the values are not specified ?


Then how should i convert these kind of text files into csv file (Comma Seperated values) Files .

Should i write my own code or is there any utilities that can help me convert these text files into csv files

Thanks in advance
Praveen
Mar 15 '07 #1
Share this Question
Share on Google+
5 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
I think a Linux tool such as awk or sed might be of more use to you in this case - you can use those to replace the tabs with the commas (or is it not currently in the format you want?).

What is the format of the current file, specifically? (Can you post two lines from it so we can get an idea?)
Mar 15 '07 #2

DeMan
100+
P: 1,806
A csv file is a file with entries delimited by a comma (which can be opened in excel among other things).

Assuming you want every tab space in the text file to be a "breaker" between columns, then it is a matter of replacing the blank with a comma. Assuming you want to split the hyphenated numbers, then you also replace the hphen with a comma (otherwise you can treat it as text).

a blank cell will be recognised if you add two commas (that is, if you need to pad out to 3 columns, just add commas until it pads far enough).
Mar 15 '07 #3

P: 19
I think a Linux tool such as awk or sed might be of more use to you in this case - you can use those to replace the tabs with the commas (or is it not currently in the format you want?).

What is the format of the current file, specifically? (Can you post two lines from it so we can get an idea?)
Thank you for your reply. I solved the problem by writing some programs .
Mar 15 '07 #4

P: 19
Thank you DeManfor the response and reply.

I wanted some clarifications regarding csv from you all

1) Should we include the column header in csv for example consider the below data

Age Location
25 California
26 London
27 Rome
28 Amsterdam

In my csv file should the header Age,Location be placed or only the data should be there .


For example which of the below is the correct format of csv file

Below Data is with Header (Age,Location)
Age, Location
25,California
26,London
27,Rome
28,Amsterdam


Now the data below this line doesn contain header , only pure data
25,California
26,London
27,Rome
28,Amsterdam


So which one of the above is in correct csv format


2)Whether each line in the csv file should end with a new line character or the data should be written into csv file continously
for example :

25,California,26,London,27,Rome,28,Amsterdam


So i will be very thankful to u all if you clarify my these doubts
Mar 15 '07 #5

DeMan
100+
P: 1,806
If the header is important then include it in the csv (as you might have noticed though, the formatting/underlining etc is ignored)
Mar 15 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.