How do I convert a tab-separated values (TSV) file to a comma-separated values (CSV) file in BASH?

后端 未结 4 1245
闹比i
闹比i 2021-02-03 14:07

I have some TSV files that I need to convert to CSV files. Is there any solution in BASH, e.g. using awk, to convert these? I could use sed, like this,

4条回答
  •  半阙折子戏
    2021-02-03 14:41

    This can also be achieved with Perl:

    In order to pipe the results to a new output file you can use the following:
    perl -wnlp -e 's/\t/,/g;' input_file.tsv > output_file.csv

    If you'd like to edit the file in place, you can invoke the -i option:
    perl -wnlpi -e 's/\t/,/g;' input_file.txt

    If by some chance you find that what you are dealing with is not actually tabs, but instead multiple spaces, you can use the following to replace each occurrence of two or more spaces with a comma:
    perl -wnlpi -e 's/\s+/,/g;' input_file

    Keep in mind that \s represents any whitespace character, including spaces, tabs or newlines and cannot be used in the replacement string.

提交回复
热议问题