I need to convert CSV to TSV in bash. I found this solution and it works well, but not for all datasets as I will show below.
For example, for a.txt
:
a,"test, part2 ""the start""",b
sed
format it badly:
[ nir ]$ cat a.txt | sed -E 's/("([^"]*)")?,/\2\t/g'
a "test Op. 15 ""the start" b
#^ tab....^ tab..................^ tab
Issues here: missing ,
, extra tab, extra quotes.
Just to reference even python code format it badly:
[ nir ]$ cat a.txt | csv2tsv.py
a "test, part2 ""the start""" b
#^ tab..........................^ tab
Issues here: extra quotes.
csv2tsv.py
is:
csv.writer(sys.stdout, dialect='excel-tab').writerows(csv.reader(sys.stdin))
Where the true convert should looks like:
a test, part2 "the start" b
#^ tab......................^ tab
Would love to get some feedback how to solve this in bash
. I went over many solution in the internet but non managed to handle those quotes on quotes inside quotes :)
tr ',' '\t' < a.txt
? – Valentin Bajrami Sep 05 '21 at 21:24tr
it will not work as it does not consider the quotes – Nir Sep 06 '21 at 05:11