-1

i have a CSV file with 3 columns. It looks like this

col1,col2,col3
123,abc , 2015-01-01
246,def , 2015-02-02

How can i remove the white space in col3 only using vim/sed?

expected output:

col1,col2,col3
123,abc ,2015-01-01
246,def ,2015-02-02
jxn
  • 7,685
  • 28
  • 90
  • 172
  • but i only want to change col3 and col2 may have commas in weird places as its text – jxn Oct 27 '15 at 18:57

2 Answers2

0

vim

In vim, this command works for your example:

%s/,[^,]*$/\=substitute(submatch(0)," ","","g")
Community
  • 1
  • 1
Kent
  • 189,393
  • 32
  • 233
  • 301
0
$ cat foobar.csv
col1,col2,col3
123,abc , 2015-01-01
246,def , 2015-02-02

$ sed 's/\(.*\),\(.*\),\(\s*\)\(.*\)/\1,\2,\4/' foobar.csv
col1,col2,col3
123,abc ,2015-01-01
246,def ,2015-02-02

Remember the -i flag if you want to edit the file in place.

timss
  • 9,982
  • 4
  • 34
  • 56
loganaayahee
  • 809
  • 2
  • 8
  • 13