JCX
JCX

Reputation: 1439

how to remove empty spaces in csv in 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.

Upvotes: 4

Views: 17281

Answers (2)

Nir
Nir

Reputation: 1426

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

Upvotes: 9

pgl
pgl

Reputation: 7981

This should work:

cp file1 file2
perl -pi -e 's/" ,"/","/g' file2

Upvotes: 1

Related Questions