Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a tab-separated file into a comma-separated file?

Tags:

unix

csv

I want to convert a tab-separated file into a CSV file. Can anyone help me?

like image 892
user400480 Avatar asked Aug 18 '10 05:08

user400480


People also ask

How do I convert tab delimited CSV to comma delimited CSV?

Select one or more lines you want to convert, or simply press Ctrl+A to select all lines. Open the File menu and choose 'Save Selected Items', or simply press Ctrl+S. From the 'Save as type' combo-box select 'Comma Delimited Text File' and ,choose or type the filename to save, and then click the 'Save' button.

How do you convert tab delimited to CSV Excel?

You can convert an Excel worksheet to a text file by using the Save As command. Go to File > Save As. Click Browse. In the Save As dialog box, under Save as type box, choose the text file format for the worksheet; for example, click Text (Tab delimited) or CSV (Comma delimited).

How do I convert a TSV file to CSV?

TSV file can be converted into CSV file by reading one line of data at a time from TSV and replacing tab with comma using re library and writing into CSV file. We first open the TSV file from which we read data and then open the CSV file in which we write data. We read data line by line.


2 Answers

The answer for OSX is different.

MacOS does not understand \t in the sed expression.

You have to insert the tab literal into the sed search pattern by using ctrl+v then tab (see How can I insert a tab character with sed on OS X?)

sed 's/ /,/g' input_file > output_file 
like image 58
ud3sh Avatar answered Sep 28 '22 12:09

ud3sh


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  
like image 26
codaddict Avatar answered Sep 28 '22 10:09

codaddict