I would like to replace the empty space between each and every field with comma delimiter.Could someone let me know how can I do this.I tried the below command but it doesn't work.thanks.

My command:
:%s//,/


53 51097 310780 1
56 260 1925 1
68 51282 278770 1
77 46903 281485 1
82 475 2600 1
84 433 3395 1
96 212 1545 1
163 373819 1006375 1
204 36917 117195 1
share|improve this question
    
Is this VIM? I don't see sed anywhere. – tjameson Nov 24 '12 at 6:05
    
Yea it's inside vim editor. – Teja Nov 24 '12 at 6:07
up vote 18 down vote accepted

If you are talking about sed, this works:

sed -e "s/ /,/g" < a.txt

In vim, use same regex to replace:

s/ /,/g
share|improve this answer
    
It does look like he's trying to use Vim here, not sed, good call – netpoetica Nov 24 '12 at 6:11

Inside vim, you want to type when in normal (command) mode:

:%s/ /,/g

On the terminal prompt, you can use sed to perform this on a file:

sed -i 's/\ /,/g' input_file

Note: the -i option to sed means "in-place edit", as in that it will modify the input file.

share|improve this answer
5  
If you are on Max OSX the -i option requires an argument. You can pass it an empty string if you don't desire a backup. – tripleee Nov 24 '12 at 8:01
    
^ thank you so much, spent a while trying to fix this command on a mac – Sean May 7 '13 at 3:02

I know it's not exactly what you're asking, but, for replacing a comma with a newline, this works great:

tr , '\n' < file
share|improve this answer

Try the following command and it should work out for you.

sed "s/\s/,/g" orignalFive.csv > editedFinal.csv
share|improve this answer

If you want the output on terminal then,

$sed 's/ /,/g' filename.txt

But if you want to edit the file itself i.e. if you want to replace space with the comma in the file then,

$sed -i 's/ /,/g' filename.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.