Escaping characters in sed - sed

I am trying to replace the value of a couple of php database array variables with sed, but it is not working as expected
Here is an example:
echo \$DB['TYPE']='MYSQL' | sed "s|^$DB['TYPE']=.*$|$DB['TYPE']='POSTGRESQL'|g"
Im trying to replace $DB['TYPE']='MYSQL' with $DB['TYPE']='POSTGRESQL'
I escaped it this way but does not work, I keep getting $DB[TYPE]=MYSQL
echo \$DB['TYPE']='MYSQL' | sed "s|^\$DB[\'TYPE\']=.*$|\$DB[\'TYPE\']=\'POSTGRESQL\'|g"
Thanks in advance

I'm trying to replace $DB['TYPE']='MYSQL' with $DB['TYPE']='POSTGRESQL'
You can use:
sed "s|\(\$DB\['TYPE'\]=\)'MYSQL'|\1'POSTGRESQL'|g" file

Reduce duplication as much as possible:
$ echo "\$DB['TYPE']='MYSQL'" |
sed "s|^\(\$DB\['TYPE'\]='\)[^']*|\1POSTGRES|"
$DB['TYPE']='POSTGRES'
You use line anchors so the g modifier is useless -- the pattern can match at most once per line.
You need double quotes on the echo line. Without them, you get the shell seeing the single quotes as quote characters, not literal characters:
$ echo \$DB['TYPE']='MYSQL'
$DB[TYPE]=MYSQL

In you example sed doen"t find the exact phrase. Why don't you just exchange the word you want to exchange like
echo \$DB['TYPE']='MYSQL' | sed "s|MYSQL|POSTGRESQL|g"
?

I ended up going with this
echo "\$DB['TYPE']='MYSQL'" | sed "s|^\$DB\['TYPE'\]=.*$|\$DB\['TYPE'\]='POSTGRESQL'|"
It works as expected. Thanks guys.

Related

Replace first word with third one in every line, but words are separated by ":"

I'm trying to learn sed but getting stuck when trying to replace first word wih the 3rd. I was thinking about the above code, but it doesn't work.
Also, is there any way of splitting the line if the words are separated by ":" using sed?
sed "s/\(^[a-z,0-9]*\) \(.*\) \([a-z,0-9]*\)/\1 \2 \1/"
From your comment below it sounds like you actually want to replace the third word with the first one rather than the other way around. If so then:
$ echo 'first:second:third' | sed 's/\(\([^:]*\).*:\).*/\1\2/'
first:second:first
or if you have many fields to manipulate:
$ echo 'first:second:third' | sed 's/\([^:]*\):\([^:]*\):\([^:]*\)/\1:\2:\1/'
first:second:first
but you should really use awk for anything involving fields anyway:
$ echo 'first:second:third' | awk 'BEGIN{FS=OFS=":"} {$3=$1} 1'
first:second:first

how to find a specific character combination and add a newline

I have a large file that looks like this
(something,something1,something2),(something,something1,something2)
how do I use sed and find ),( and replace it with );( or add a newline between the parentheses that has a comma character.
I did try sed 's/),(/),\n(/g' filename.txt but for some reason it does not work
for those who come here and want to know how this work without getting a lot of stackoverflow "greetings"
since I was on Mac os x you need to replace your \n with \'$'\n''
so to find ),( and add a new line between the parentheses this is the command I used
sed 's/;/\'$'\n''/g' testdone.txt > testdone2.txt
ES
echo "(something,something1,something2),(something,something1,something2)" | sed "s|),(|);(|"
This prints the below for me.
(something,something1,something2);(something,something1,something2)
For new line
echo "(something,something1,something2),(something,something1,something2)" | sed "s|),(|)\n(|"
And the above prints the below.
(something,something1,something2)
(something,something1,something2)

sed replacing special string quota

sed is still giving me headaches, so a little help is extremely appreciated.
In a file I have a string like:
SOME_TEXT="variables"
What I want to accomplish is to add a piece of text (variable) to either the end or the begging of the string for that text.
I tried to use variations of:
sed -i '/^SOME_TEXT="/ s/$/ SOME_TEXT="new text'/' filename
but that is failing, so clearly the quota for the string I want to add to is messing up the syntax.
LE:
A variation further is that I have a variable that I want to use as the replace in that syntax, so I have this:
sed -i "s/^SOME_TEXT="/SOME_TEXT=" $variable/" file
This actually produces this output, as it picks up incorrectly the opening/closing quotas:
SOME_TEXT = text_variable" initial text continuation
So how can I properly close the trailing quota so that I can use the variable after it?
I used
sed 's/^SOME_TEXT="/SOME_TEXT="new text/' filename
and it showed:
SOME_TEXT="new textvariables"
Is that what you want?
Escape the '"' characters with a '\' so that they don't terminate your regex string.
sed -i "s/^TEXT=\"/TEXT=\" $variable/"

Sed remove multiple characters

I would like to replace multiple characters
echo "R \e&p[%20])l(a/ce" | sed 's|%20|-|g;s|\[||g;s|]||g;s| ||g;s|#||g;s|/||g;s|)||g;s|(||g;s|&||g;s|\\||g'
Rep-lace
Is there another way of doing so or is this it?
Replace %20 with - and the rest with nothing
I'd use
echo "R \e&p[%20])l(a/ce" | sed 's/%20/-/g; s/[][ #/()&\\]//g'
Because the character set is easier to extend that way. The thing to know is that ] has to be the first character in the set to be recognized as part of the set rather than the closing bracket.
Depending on what exactly it is you want to do, it may be worth a thought to invert the character set instead and replace everything but a specified number of characters. For example:
echo "R \e&p[%20])l(a/ce" | sed 's/%20/-/g; s/[^-[:alnum:]]//g'
This will replace %20 with - and then remove all characters except - and alphanumeric characters.
In Bash you can use in Parameter Expansion + sed:
bash$ STR="R \e&p[%20])l(a/ce"
bash$ echo "${STR/"%20"/-}" | sed -r 's/[^a-z-]//gi'
Rep-lace

How to globally replace strings in lines NOT starting with a certain pattern

I want to globally replace the string foo with the string bar, using sed. This should only be done for lines which do NOT start with the string ##Input.
I can't get it to work. I tried things like this but reached a point where I'm not sure if I know what I'm doing:
sed -i '/^##Input/ s/foo/bar/g' myfile
Please help!
You just need to negate the match using !:
sed -i '/^##Input/! s/foo/bar/g' myfile
You got to escape # as in \#.
An ugly answer for an ugly request (i.e. they get what they asked for):
echo \{
for file in *.json; do
sed -n '/^[\{\}]/! s/\([^\,]\)$/\1,/; /^[\{\}]/!p' $file
done
echo \{