Convert Separator Character While Escaping Fields in CSV

csvtext formatting

I have a text file with fields delimited by |. I want to convert it to , like a typical CSV. I have tried to use this:

sed 's/|/,/g' test.txt > test.csv

but some of the fields already have commas in them. For example:

var1|var2|var3
Potter, Harry|2|3

What should I do?

Best Answer

Try this

sed 's/|/","/g; s/^/"/; s/$/"/' file 

This will produce output like this:

"var1","var2","var3"
"Potter, Harry","2","3"

Explanation:

You need to enclose each of your column using quotes "" .

  • First s/|/","/g replaces all | with "," comma enclosed in quotes Output : var1","var2","var3 Potter, Harry","2","3

  • Second s/^/"/ replaces the beginning of line with quotes where ^ means beginning of line

  • Third s/$/"/ replaces the end of line with quotes where $ means end of line

You can also escape the commas in field using backslash before each comma something like this

Potter\, Harry,2,3

You can read more about csv formatting here: https://www.csvreader.com/csv_format.php

Related Question