sed with literal string--not input file

amphibient picture amphibient · Oct 24, 2012 · Viewed 103.7k times · Source

This should be easy: I want to run sed against a literal string, not an input file. If you wonder why, it is to, for example edit values stored in variables, not necessarily text data.

When I do:

sed 's/,/','/g' "A,B,C"

where A,B,C is the literal which I want to change to A','B','C

I get

Can't open A,B,C

As though it thinks A,B,C is a file.

I tried piping it to echo:

echo "A,B,C" | sed 's/,/','/g' 

I get a prompt.

What is the right way to do it?

Answer

Gilles Quenot picture Gilles Quenot · Oct 24, 2012

You have a single quotes conflict, so use:

 echo "A,B,C" | sed "s/,/','/g"

If using , you can do too (<<< is a here-string):

sed "s/,/','/g" <<< "A,B,C"

but not

sed "s/,/','/g"  "A,B,C"

because sed expect file(s) as argument(s)

EDIT:

if you use or any other ones :

echo string | sed ...