I am using the following command line in Linux to save the content of my input file (txt file consisted of columns) as a spreadsheet:

less input_file > out_put.csv

my output file is enter image description here

The problem is in the output spreadsheet; all the columns in the input file stuck together in one column of the output file (spreadsheet CSV).

How can I revise my simple code to make it more efficient to save my txt file as a tab delimited text and a spreadsheet.

  • 1
    can you post (example) output data of less input_file – Pandya Oct 16 '14 at 12:19
  • 1
    Please don't post screenshots of text. Copy the text and use the provided formatting tools to include it in your question instead. That way, we can actually copy it and try to answer your question. In addition, you need to tell us what program you are using to see the spreadsheet and if you are telling it to use space as a column separator (hint: you aren't). – terdon Oct 16 '14 at 12:36

First of all, less is just a pager, it is a tool that lets you read files. What you're doing is exactly the same as copying input_file to out_put.csv (cp input_file out_put.csv). You're not changing the content in any way.

So, to read it as a spreadsheet using, for example libreoffice, you would need to open your spreadsheet application, then open your input_file and use space as the column separator:

        libreoffice text import dialog with spaces chosen as separator

Now, if you really want to convert your file to comma separated values format (.csv), you would need to add commas. This command will replace all spaces with commas on each of your lines and save the output as output.csv:

sed 's/  */,/g' input_file > output.csv

The command above is sed and here I am using its substitution operator. The general format is s/pattern/replacement/ which will replace pattern with replacement. The g at the end makes it replace all occurrences of the pattern on each line, without it, it would only replace the first. The pattern I gave it was (a space) followed by 0 or more (that's what * means) spaces (*) and I told it to replace with ,. This basically means "replace any occurrences of one or more spaces with a comma".

  • Thanks BUT The output file is totally distorted please check – user88036 Oct 16 '14 at 12:54
  • @MJA yes, it looks like you didn't use two spaces (sed 's/ */,/g') as I did but one (`sed 's/ */,/g'). Please copy/paste the command directly to your terminal. – terdon Oct 16 '14 at 13:03
  • @MJA that is magic :). It's called sed and here, I'm using the substitution operator. See my updated answer for an explanation of how the magic works. – terdon Oct 16 '14 at 13:12
  • 1
    @DigitalTrauma exactly :) And -r is a GNU extension so I thought I'd keep it simple. – terdon Oct 16 '14 at 14:58
  • 1
    @MJA just make sure they are there in your input. – terdon Oct 17 '14 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy