0

How do I create a CSV file from a TXT file as per the following example? Thanks.

TXT file:
This is line number one
This is line number two with extra words
This is line number three with even more words
Resulting CSV file:
Col#1 Col#2 Col#3 Col#4  Col#5 Col#6 Col#7 Col#8 Col#9
This  is    line  number one
This  is    line  number two   with  extra words
This  is    line  number three with  even  more  words

Edit: Well, my question is different from and hence not a duplicate of:

https://askubuntu.com/questions/1214146/trying-to-create-a-csv-file-from-a-text-file-in-ubuntu

because the other question is asking to "organize" each line in to "columns", while my question is about organizing each word (separated by whitespaces) into a new column. Thanks.

Jags
  • 2,126
  • 2
  • 19
  • 37
  • 1
    Are you trying to achieve this with a terminal? with a text editor? Any additional information on what are you trying is important to receive better support here. Thank you. – Geppettvs D'Constanzo Mar 01 '20 at 23:14
  • 1
    What have you tried so far? Please show the code, and the results. – K7AAY Mar 01 '20 at 23:14
  • @geppettvs-dconstanzo yes through terminal. @k7aay I just came across this `https://askubuntu.com/questions/1214146/trying-to-create-a-csv-file-from-a-text-file-in-ubuntu` and thought if I could create a CSV file as described in my question. Thanks. – Jags Mar 01 '20 at 23:16
  • Does this answer your question? [Trying to create a CSV file from a text file in Ubuntu](https://askubuntu.com/questions/1214146/trying-to-create-a-csv-file-from-a-text-file-in-ubuntu) – K7AAY Mar 01 '20 at 23:21
  • 1
    @k7aay No. I just posted the link to the same question in the comment right above yours. In fact, I asked this question via comment on that page but "steeldriver" said mine is a different question. So I asked a new question in here. Thanks. – Jags Mar 01 '20 at 23:24
  • 1
    It depends what you mean by a CSV file - if you simply want to convert horizontal whitespace to commas you can use `tr -s '[:blank:]' , < file.txt` OTOH if you want a header line, `NULL`s for missing fields, etc. it will be more complicated – steeldriver Mar 01 '20 at 23:25
  • You can also try with `sed` with a command like this: `sed 's/ /,/g;' file`. This will show the results on screen. If you may wish to write a new file with the results try this: `sed 's/ /,/g;' file > newfile`. Nevertheless this solution will replace any spaces with commas. And as I see in your question, you may need some words to be treated like sentences or several words in one CSV field. I don't know how to handle this so far. – Geppettvs D'Constanzo Mar 01 '20 at 23:27
  • @geppettvs-dconstanzo Yes, this is working just fine: `sed 's/ /,/g;' txt_file.txt > csv_file.csv` Thanks. – Jags Mar 01 '20 at 23:33
  • Does 'column -t' work for you? It does not add the header line. – hojusaram Mar 02 '20 at 06:44

1 Answers1

4

It depends what you mean by a "word", and what you mean by a "CSV file".

If you just want to convert horizontal whitespace to commas, you could use something like

tr -s '[:blank:]' , < file.txt > file.csv

or

sed 's/[[:blank:]]\{1,\}/,/g' file.txt > file.csv

or

awk '{$1=$1} 1' OFS=, file.txt > file.csv

If you want a header row (especially if it needs to count the maximum number of columns) and / or things like `NULL's for unpopulated fields, it will be more complicated.

steeldriver
  • 131,985
  • 21
  • 239
  • 326
  • as I do not need headers, all three options are working just fine. I've just accepted the answer. Thanks alot. – Jags Mar 01 '20 at 23:37