Find the answer to your Linux question:
Results 1 to 4 of 4
I have a text file, which contains two columns of data - I've also split it with cut, so I could just as easily work from two files, each with ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    5

    Creating a CSV file from columns in text file

    I have a text file, which contains two columns of data - I've also split it with cut, so I could just as easily work from two files, each with one column.

    I need to combine them to form a CSV file, currently I'm doing it thus:
    Code:
    while read line; do
    	echo $line | cut -c 1-5 | tr -d '\000''\004''\012' >> data.csv
    	echo -ne "," >> data.csv
    	echo $line | cut -c 7-12 | tr -d '\000''\004''\012' >> data.csv
    	echo -ne "\n" >> data.csv
    done </tmp/data
    You can see I'm also using tr to do some work. The problem is this script has to run on cygwin, and cygwin is very slow at loading programs. Each call to tr and cut takes ages, so this loop, operating on files of hundreds or thousands of lines, takes a very long time. What I need to try to do, is find a way to achieve the same result, without using a loop.

    Is there any way to do this without looping?

  2. #2
    Linux User sgosnell's Avatar
    Join Date
    Oct 2010
    Location
    Baja Oklahoma
    Posts
    358
    Loading the initial file in OpenOffice or LibreOffice calc, and saving as .csv, should do it easily. Reinventing the wheel is not a productive exercise.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    5
    High volume data coming from a spectrometer. But never mind, the solution is to use sed,and regular expressions to take care of things.

    Thanks anyway.

  4. #4
    Linux User sgosnell's Avatar
    Join Date
    Oct 2010
    Location
    Baja Oklahoma
    Posts
    358
    Ah, that makes a difference. Yes, sed is probably the tool of choice for streaming data.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...