|
Home > Archive > Unix Shell > October 2006 > how to create a csv file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to create a csv file
|
|
| donghuang.de@googlemail.com 2006-10-23, 1:16 pm |
| hello folks,
as the subject mentioned i am trying to parse a text file into a csv
file.
the text file looks like this
mike
22
male
ee2222
may
21
female
ee323
the csv or the tabel looks at this
name age sex course
mike 22 male ee2222
may 21 female ee323
with tabulator between them.
i tried with "sed", but donot find a way to replace the "^" with
tabulator, but it comes out with
mike
22
male
ee2222
anyone who has an idea?
thanks!
Dong
| |
| Eric Moors 2006-10-23, 1:16 pm |
| donghuang.de@googlemail.com wrote:
> hello folks,
>
> as the subject mentioned i am trying to parse a text file into a csv
> file.
>
> the text file looks like this
> mike
> 22
> male
> ee2222
> may
> 21
> female
> ee323
>
> the csv or the tabel looks at this
>
> name age sex course
> mike 22 male ee2222
> may 21 female ee323
>
> with tabulator between them.
csv = comma separate values, so you ought to be using a comma
instead of a tab as a delimiter to get a csv file.
j=0
unset out
IFS='
'; while read a
do
out="${out}${out:+,}\"${a%}\""
(( j++ ))
[[ $j == 4 ]] && { echo "$out"; unset out; j=0;}
done < t
replace the comma with a tab to get tab-delimited output.
This does not allow you to have newline characters in a field.
Various other characters (space, comma etc.) should be fine.
Eric
| |
| donghuang.de@googlemail.com 2006-10-23, 1:16 pm |
| hi eric,
first thanx for your reply.
i am a newbie in the shell scripting field. i tried to understand your
code and did modification to match my code.
a=TMP.txt
j=0
unset out
IFS='
'; while read a
do
out="${out}${out:+,}\"${a%}\""
(( j++ ))
[[ $j == 4 ]] && { echo "$out"; unset out; j=0;}
done < t
what i am not sure is
1. whether the variable "a" is the input text file?
2. the meaing of "< t" at the end
i tried to run this. the t "no such file or directory"
what for is it?
btw. can you give me a good online tutorial link about shell script?
Thanks
Dong
On Oct 23, 3:34 pm, Eric Moors <scare.c...@oz.land> wrote:
> donghuang...@googlemail.com wrote:
>
>
>
>
>
> instead of a tab as a delimiter to get a csv file.
>
> j=0
> unset out
> IFS='
> '; while read a
> do
> out="${out}${out:+,}\"${a%}\""
> (( j++ ))
> [[ $j == 4 ]] && { echo "$out"; unset out; j=0;}
> done < t
>
> replace the comma with a tab to get tab-delimited output.
>
> This does not allow you to have newline characters in a field.
> Various other characters (space, comma etc.) should be fine.
>
> Eric
| |
| Eric Moors 2006-10-23, 1:16 pm |
| donghuang.de@googlemail.com wrote:
> hi eric,
>
> first thanx for your reply.
>
> i am a newbie in the shell scripting field. i tried to understand your
> code and did modification to match my code.
I see, I forget to create som more meningfull names.
> a=TMP.txt
Nope, a is just a variable name. Leave it undefined at this point.
First the script initializes j, out and IFS
> j=0
> unset out
> IFS='
> '; while read a
read a line and put the result in variable "a"
> do
> out="${out}${out:+,}\"${a%}\""
append a to the output line (out), add a comma if needed
${out:+,} means that if out is not empty, a comma should be inserted
> (( j++ ))
increment a counter
> [[ $j == 4 ]] && { echo "$out"; unset out; j=0;}
if the required number of fields is reached, echo an output line
and reset all variables
> done < t
t is the name of the input file, in your case this should read TMP.txt
> what i am not sure is
> 1. whether the variable "a" is the input text file?
> 2. the meaing of "< t" at the end
> i tried to run this. the t "no such file or directory"
> what for is it?
>
> btw. can you give me a good online tutorial link about shell script?
checkout www.shelldorado.com for loads of examples and links.
Eric
| |
| John W. Krahn 2006-10-23, 1:16 pm |
| donghuang.de@googlemail.com wrote:
>
> as the subject mentioned i am trying to parse a text file into a csv
> file.
>
> the text file looks like this
> mike
> 22
> male
> ee2222
> may
> 21
> female
> ee323
>
> the csv or the tabel looks at this
>
> name age sex course
> mike 22 male ee2222
> may 21 female ee323
>
> with tabulator between them.
>
> i tried with "sed", but donot find a way to replace the "^" with
> tabulator, but it comes out with
> mike
> 22
> male
> ee2222
>
> anyone who has an idea?
$ echo "mike
22
male
ee2222
may
21
female
ee323" | PERL -lpe'$\=++$a%4?"\t":$/'
mike 22 male ee2222
may 21 female ee323
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Ed Morton 2006-10-23, 1:16 pm |
| donghuang.de@googlemail.com wrote:
> hello folks,
>
> as the subject mentioned i am trying to parse a text file into a csv
> file.
>
> the text file looks like this
> mike
> 22
> male
> ee2222
> may
> 21
> female
> ee323
>
> the csv or the tabel looks at this
>
> name age sex course
> mike 22 male ee2222
> may 21 female ee323
>
> with tabulator between them.
<snip>
$ awk 'ORS=(NR%4?"\t":"\n")' file
mike 22 male ee2222
may 21 female ee323
Regards,
Ed.
|
|
|
|
|