DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Text files to csv file conversion in Linux

  1. #1
    Join Date
    Feb 2007
    Posts
    18

    Smile Text files to csv file conversion in Linux

    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 :)

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,
    if you work on Unix/Linux - style OS or Cygwin on Windows you can use the "awk" utility. It's made for this sort of problem. You can also try "sed" or if you have spare time to invest in learning have a look into lex/yacc and it's clones. There are a few other utilities, too.
    But for your problem at hand a simple C++ routine should do the trick.
    Just open your original file for read and an output file for write. Then read line by line and write all the characters until you encounter the first delimiter ("\t" or "::"), then write a comma, skip the delimiter and read to the next delimiter and so on... If you know the maximum number of colums, fill any line that has not enough columns with commas then enter a linebreak ("\n"). When you save this file with csv extension you should be able to open it with starOffice or something like that.
    Cheers,
    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    open the file in openoffice using space delimited or custom or whatnot and save back out if there are only a few.

    csv is just a comma "," between fields in a text file, nothing special here, many ways to get this done.

  4. #4
    Join Date
    Feb 2007
    Posts
    18
    Thanks a lot for you both for replying

    I solved the problem by writing some scripts and generating csv files


    Praveen

Similar Threads

  1. Randomized Results from multiple text files
    By LordNightwinter in forum VB Classic
    Replies: 1
    Last Post: 09-06-2006, 03:15 PM
  2. VS.Net and Code Behind
    By Chaitanya Marvici in forum ASP.NET
    Replies: 6
    Last Post: 07-21-2003, 09:15 AM
  3. Replies: 1
    Last Post: 12-02-2002, 05:27 PM
  4. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 05:34 PM
  5. Replies: 0
    Last Post: 04-17-2000, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center