Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing all commas with spaces - S/R with a Unix Command?

Tags:

replace

unix

I'm looking for a Unix command that will allow me to search/replace in a file - I need to replace all commas in a certain file with spaces. I need to do this in a script and I'm looking to avoid parsing/reading the file line by line. Is there a simple unix command that will allow me to do this?

like image 997
Amir Rachum Avatar asked Mar 29 '11 08:03

Amir Rachum


People also ask

How do I change commas with spaces in Linux?

The command s/,/ /g tells sed to replace every comma with a space. q tells it to quit after the first line. The lone < indicates redirection: it tells the shell to get its input for the read command from the file-like object that follows on the command line.

How do you replace multiple spaces with commas in Unix?

Simple SED commands are: sed s/ */ /g This will replace any number of spaces with a single space. sed s/ $// This will replace any single space at the end of the line with nothing. sed s/ /,/g This will replace any single space with a single comma.


2 Answers

sed 's/,/ /g' filename >resultfile

like image 133
khachik Avatar answered Oct 23 '22 19:10

khachik


You can use awk, sed, vi, ex or even Perl, PHP etc ... depends what you are proficient with.

sed example:

sed -i 's/,/ /g' filename_here
like image 25
Konerak Avatar answered Oct 23 '22 21:10

Konerak