Skip to content
Advertisement

Convert CSV to TSV

How do you convert this csv file into a tab delimited file?

"Country","Percent","Percent of patients","home health","home health agency","friends and family","Surveys","Response"
"Nation","88","85","83","84","78",,

Notice both the Surverys and Response columns are empty strings.

I use this code to convert it to a tab file –

sed 's/","/"t"/g'
sed 's/,,/t""t/g'

It doesn’t convert the last column though. This is the output I get (Notice the last column is omitted) –

"Country"   "Percent"   "Percent of patients"   "home health"   "home health agency"    "friends and family"    "Surveys"   "Response"
"Nation"        "88"    "85"    "83"    "84"    "78"    ""

There are 8 columns in the header and only 7 columns in the tab delimited data, so the last column is missed.

UPDATE

My column names have commas in them.

Advertisement

Answer

With GNU awk.

awk 'BEGIN{FS="",""; OFS="t"} {FS=","; for(i=1; i<=NF; i++) {gsub(/"/,"",$i); $i=""" $i """} print}' file

Output:

"Country"      "Percent"       "Percent of patients"   "home health"   "home health agency"    "friends and family"       "Surveys"       "Response"
"Nation"        "88"    "85"    "83"    "84"    "78"    ""      ""
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement