Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove empty spaces in csv in unix

Tags:

unix

Hi I would like to use awk or sed to remove blanks spaces before a comma in a CSV file. Example input file would be:

"abcd"  ,"test 123"

Output file:

"abcd","test 123"

Only remove white space before a comma but not in between words. looking for trim function in unix. Please help. Thank you very much.

like image 913
JCX Avatar asked Jan 19 '23 01:01

JCX


2 Answers

This can be easily done with sed:

sed 's/ *,/,/' csv-file

This tells sed to remove sequences of whitespace characters (of any length) before commas.

Notes:

  1. The assumption here is that commas are illegal within the fields (i.e "test, 123" is illegal).

  2. As you requested, this removes whitespaces only before commas. if you want to remove whitespaces which are after commas as well:

    sed 's/ *, */,/' csv-file

like image 198
Nir Avatar answered Jan 29 '23 14:01

Nir


This should work:

cp file1 file2
perl -pi -e 's/" ,"/","/g' file2
like image 28
pgl Avatar answered Jan 29 '23 16:01

pgl