sed and replace

Hi All,

I am trying to replace ", " with "," . For I am getting below error. I think I am missing something. I tried to escape the , with \ still I see the same error

Input
110|3|401|0|CANDY, GUM, MINTS|GUM|SWENT|||||
Output
110|3|401|0|CANDY,GUM,MINTS|GUM|SWENT|||||

Command Tried
sed -i s/, //g  myfile
sed -i s/\, //g myfile

sed: -e expression #1, char 3: unterminated `s' command

------ Post updated at 08:20 AM ------

Never mind. Got it worked as below

echo "CANDY, GUM, MINTS|GUM" | sed  s/,\ /,/g

Just use single quotes around the sed script and you'll get rid of most interference by the shell:

sed 's/, /,/g'

You have seen this code which is perfectly very helpful for your error.
In this code you can check where is your mistake.
APPNAME="Test Application"

_OLD=";newrelic.appname = \"PHP Application\""
_NEW="newrelic.appname = \"${_APPNAME}\""

sed -i 's/$_OLD/$_NEW/g' /etc/php.d/newrelic.ini

Within 'ticks' (also named single quotes) everything but ' is literal.
Within "quotes" (also named double quotes) the shell substitutes $variables with their values.
Try "s/$_OLD/$_NEW/g"

1 Like