-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,087
  • 26
  • 84
  • 159
  • 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
  • 181,427
  • 30
  • 222
  • 283
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,692
  • 3
  • 33
  • 55
loganaayahee
  • 779
  • 2
  • 7
  • 13