So much for the perl script -- let's suppose you save that in a file called "space2comma.pl"; using the command line to handle all the file i/o for you involves a shell command like this:#!/usr/bin/perl # simple process to convert spaces and tabs to commas while ( <> ) { tr/\t /,/; print; }
(I personally like making sure that the output file does not replace the input file, in case I find out that I need to fix the script or change my plan about what it's supposed to do.)space2comma.pl input.file > output.file
I hope you're going to be careful about getting to know the input data, and knowing what sorts of conditions the output data needs to meet. Maybe the output is supposed to have a fixed number of comma-separated fields? Maybe if the input contains some commas already, these should be treated as data (which means the fields that contain commas need to be quoted in the csv output)? Maybe if two "fields" are separated by more than one space or tab character, that whole whitespace string should be treated as a single field boundary -- so add a final "s" modifier to the "tr///" (or maybe as multiple boundaries with empty fields between them, so don't use "tr///s")?
Getting that sort of stuff wrong can be a real drag.
In reply to Re: convert txt file to csv file
by graff
in thread convert txt file to csv file
by aztec
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |