Trying to escape a / for sed on ubuntu - sed

I am trying to use sed to replace a path in a file.
sudo sed 's/a/b/g' -i /tmp/test
However the variable is
a = /var/lib and
b = /data/lib
How do I escape the slash?

The character just after the s command doesn't need to be a /. When working with paths, I use :, as in:
sudo sed 's:a:b:g' -i /tmp/test

this should work
sed -i "s#$a#$b#g" /tmp/test
two things you need to take care about:
1) if you want to use variables in your sed line, use double quotes
2) delimiter could be other than "/", e.g. #, #, : ...

You can change sed's delimiter for instance use # instead:
$ sed 's#/var/lib#data/lib#g'
/var/lib
data/lib

In sed(1), as in vi(1), the '/' is just the customary separator. It can be escaped with \, leading to "leaning toothpick syndrome" when munging path names:
sed -e 's/\/var\/lib/data\/lib/' ...
You can use another non-word character, e.g. ';':
sed -e 's;/var/lib;data/lib; ...

Related

how to use sed to replace all backslashes with double quote?

I am trying to use this to replace all backslashes with double quotes.
sed -e "s/'\\\\''/\"/g" nomefile
this doesn't do anything.
Input: "prova d\"amico"
Desired output: "prova d""amico"
sed -e 's/\\/"/g'
Will replace all / with "
Your issue is in the first part: \\\''/, you're looking for ' surrounded /, but you just need \\ for an escaped \
➜ cat input
"prova d\"amico"
➜
➜ sed -e 's/\\/"/g' input
"prova d""amico"
➜
There's a command specifically for global character substitutions ("transliterations"), y:
sed 'y/\\/"/'
where \ has to be escaped with another \.

How to add quote at the end of line by SED

sed -i 's/$/\'/g'
sed -i "s/$/\'/g"
How to escape both $ and ' by 1 command?
This might work for you (GNU sed):
sed 's/$/'\''/' file
Adds a single quote to the end of a line.
sed 's/\$/'\''/' file
Replaces a $ by a single quote.
sed 's/\$$/'\''/' file
Replaces a $ at the end of line by a single quote.
N.B. Surrounding sed commands by double quotes is fine for some interpolation but may return unexpected results.
Use octal values
sed 's/$/\o47/'
Care to use backslash + letter o minus + octal number 1 to 3 digit
Just don't use single quotes to start the sed script?
sed "s/$/'/"
The /g at the end means to apply everywhere it's found on each stream (line) - you don't need this since $ is a special character indicating end of stream.
To add a quote at the end of a line use
sed -i "s/$/'/g" file
sed -i 's/$/'"'"'/g' file
See proof.
If there are already single quotes, and you want to make sure there is single occurrence at the end of string use
sed -i "s/'*$/'/g" file
sed -i 's/'"'*"'$/'"'"'/g' file
See this proof.
To escape $ and ' chars use
sed -i "s/[\$']/\\\\&/g" file
See proof
[\$'] - matches $ (escaped as in double quotes it can be treated as a variable interpolation char) or '
\\\\& - a backslash (need 4, that is literal 2 backslashes, it is special in the replacement), and & is the whole match.

Use sed to replace complex line using environment variable

I have a file: foo
The file has a line:
JVMDATA="$(${TIMEOUT} sudo /usr/lib/jvm/jdk1.8.0_92/bin/java -jar ${JVMINSPECTOR} ${PID} 2>&1)"
I would like to replace /usr/lib/jvm/jdk1.8.0_92/bin/java with:
JAVA_HOME/bin/java
How do I do this?
One way to do it could be this:
sed -e 's|/usr/lib/jvm/jdk1.8.0_92/bin/java|JAVA_HOME/bin/java|' original.txt > new.txt
cp original.txt original.txt.sav
mv new.txt original.txt
This uses the sed pattern substitution s/old/new/.
Normally it is a pain to do with slashes because you need to escape them.
On my system sed tolerates vertical-bar pattern delimiters so that can make it more readable.
I suspect you're going to want to change it to $JAVA_HOME... (note the leading dollar sign).
Just for completeness, here's a version using the more common slash pattern delimiter:
sed -e 's/\/usr\/lib\/jvm\/jdk1.8.0_92\/bin\/java/JAVA_HOME\/bin\/java/' original.txt > new.txt

Substitute with backslash in sed

I want to replace word with \word{sth} with sed.
I type in
sed -i s#word#\\word{sth}
but i am getting is word{sth} instead of \word{sth}
I tried with 1 slash also in the command
you should add four backslashes.
you need two to escape the backslash by the terminal, and two to escape it for sed. 2*2=4.
$ echo word|sed s#word#\\\\word{sth}#gi
\word{sth}
Consider enclosing sed expression with single-quotes '
sed -i 's#word#\\word{sth}#' file

how to find replace value with whitespace using sed in a bash script

I have values in a file like this ' value-to-remove '(without the ' characters). I want to use sed to run through the file and replace the values including the space before and after. I am running this via a bash script.
How can I do this?
The sed command I'm using at the moment replaces the values but leaves behind the two spaces.
sed -i 's/ '$value' / /g' test.conf
In script I have
sed -i -e 's/\s'$DOMAIN'-'$SITE'\s/\s/g' gitosis.conf
echoed as
sed -i -e s/\sffff.com-eeee\s/\s/g test.conf
Not working though.
IMHO your sed does not know '\s', so use [ \t], and use double quotes, otherwise your variables will not expand. e.g.:
sed -i -e "s/[ \t]'$DOMAIN'-'$SITE'[ \t]/ /g" gitosis.conf
Let me know if this is what you need
echo 'Some values to remove value-to-remove and more' | sed -e 's/\svalue-to-remove\s/CHANGED/g'
output: Some values to removeCHANGEDand more