0

I need to replace a word between the quotes after equal sign using sed. but I'm unable to do it please suggest? #mfsbsd.rootpwhash="" look like #mfsbsd.rootpwhash="wsnk&*32jk"

cat loader | sed -e 's|["\'']||g' | sed 's/rootpwhash="[^"]*"/rootpwhash="abcd"/'

3 Answers3

0

Maybe a matter of systematically escaping the " with the \ character? This works for me:

$ echo Name=\"\" | sed -e 's/\"\"/\"This should be filled in\"/'Name="This should be filled in"

To replace anything that is originally between the "":

$ echo Name=\"Original\" | sed -e 's/\".*\"/\"This should be filled in\"/'
Name="This should be filled in"
vanadium
  • 88,010
  • wow!nice. but i need user variable replaced word, im getting unmatched ''' error, echo mfsbsd.rootpwhash=" " | sed -e "s/".*"/$text/" – Sreekanth Chityala Mar 09 '21 at 18:01
  • That is not in your question. If, after having an answer on this question ("how to replace a word between double quotes?" is what you asked!) specific issues remain, then outline the specific issue in a new, specific question. It is not allowed here to change the nature of your question after the fact, so feel free to ask a different question. – vanadium Mar 10 '21 at 09:02
0

If your key-value pair is structured one per line, you only need to match the key-part. i.e., if you don't have to match the value too.

text=new; cat loader | \
sed "s/\(mfsbsd.rootpwhash\)=.*/\1=\"$text\"/"
  • good, mfsbsd.rootpwhash=sri its printing like this with cat loader | sed "s/(mfsbsd.rootpwhash)=.*/\1="$text"/" . how can i add double qoute to the string(sri) – Sreekanth Chityala Mar 09 '21 at 18:07
  • /\1=\"$text\"/" –  Mar 09 '21 at 18:09
  • sorry to ask precise answer im new to scripting. i tried this not working cat loader | sed "s/(mfsbsd.rootpwhash)=.*/\1="$text"/ printing mfsbsd.rootpwhash=sri" before s not coming another double qoute. – Sreekanth Chityala Mar 09 '21 at 18:19
  • looks like you have missed the last quote –  Mar 09 '21 at 18:20
  • sed: 1: "s/"."/"%6%GOrxqIbx.CmZ ...": bad flag in substitute command: 'l' im getting this error always. when im running this sed -i -e "s/(mfsbsd.rootpwhash)=./\1="$newpass"/" /bootpool/boot/loader.conf whenever i run this getting error bad flag in substitute command: 'l' with different letter which is not exists in my command. any sol – Sreekanth Chityala Mar 09 '21 at 20:09
  • if you are going to do an in-place substitution you need to place "loader" as an input file instead text=new; sed -i 's/\(mfsbsd.rootpwhash\)=.*/\1="'"$text"'"/' loader –  Mar 09 '21 at 20:26
  • @Sreekanth Chityala use `` when commenting code ... –  Mar 09 '21 at 20:32
0

You can use double quotes, and escape special characters in your substitute variable

$ export TESTVAR="wsnk\&\*32jk"
$ cat loader | sed -e "s/\".*\"/\"$TESTVAR\"/"
#mfsbsd.rootpwhash="wsnk&*32jk"