Sed with a literal string - not an input file

This should be easy: I want to run sed for a string string, not for the input file. If you are wondering why, for example, editing values ​​stored in variables, text data is optional.

When I do this:

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

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

I get

 Can't open A,B,C 

As if he thinks A, B, C is a file.

I tried to execute its echo:

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

I get an invitation.

What is the right way to do this?

+54
linux unix sed
Oct 24
source share
3 answers

You have one quote conflict, so use:

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

If you use bash , you can ( <<< - here-string ):

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

but not

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

because sed expects the file as argument (s)

EDIT

if you use ksh or any others:

 echo string | sed ... 
+80
Oct 24 '12 at 19:00
source share

It works the way you want:

 echo "A,B,C" | sed s/,/\',\'/g 
+6
October 24 '12 at 19:01
source share

My version using variables in a bash script:

Find any backslashes and replace them with a slash:

 input="This has a backslash \\" output=$(echo "$input" | sed 's,\\,/,g') echo "$output" 
+2
Jun 07 '16 at 14:57
source share



All Articles