views:

21

answers:

4

Hi

i want to convert a tab separated file into csv file

can anyone help me

+1  A: 

Try replacing all the tabs with commas.

Possibly with a regex like s/\t/,/g, if you don't have any quoted fields.

Or, you know, Excel could do that for ya. Or R. Or anything which can take in a TSV file.

Borealid
A: 

In unix:

sed -i -e 's/\t/,/g' filename
Peter
+1  A: 

You can use sed as:

sed 's/\t/,/g' input_file > output_file

This will keep the input file unchanged and will create a new file output_file with the changes.

If you want to change the input file itself without creating a new file you can use -i option to sed to do inplace changes:

sed -i 's/\t/,/g' input_file 
codaddict
A: 

Is this the usual easy-question-all-languages thing? Okay, here is my haskell solution:

main = interact (unlines . replTab . lines) where
  replTab l = l       >>= (\line ->
    "\"" ++ line "\"" >>= \char ->
    case char of
      '\t' -> "\",\""
      '"'  -> "\"\""
      _    -> [char]
    )

not tested, but should work.

PS: All the other solutions aren't aware of escaping commas.

FUZxxl