Home > OS >  how to append with sed when pattern is a variable
how to append with sed when pattern is a variable

Time:10-06

I am trying to execute the next code:

P=$(grep -Ei "^export PATH" "$HOME"/.zshrc)
sed '/'"${P}"'/ a Hello_World' $HOME/.zshrc

But i get the next error:

sed '/'"${P}"'/ a Hello_World' $HOME/.zshrc
sed: can't find label for jump to `in/DevTools/flutter/bin/:$HOME/bin/DevTools/Android/Sdk/platform-tools/:/usr/local/go/bin/'

The objective is append the /usr/local/go/bin, with the existing PATH in the user .*rc (bash,zsh,fish).

other thing is i wanna, make this work with variable, because if the user wanna change the location, will do to the Variable instead of the line.

thanks.

CodePudding user response:

The objective is append the /usr/local/go/bin, with the existing PATH in the user .*rc (bash,zsh,fish).

Sooo, do exactly that, for example:

echo 'export PATH="$PATH:/usr/local/go/bin"' >> .zshrc

Doing grep -Ei "^export PATH" is very much not enough, I could have:

: <<COMMENT
export PATH=haha
COMMENT

You can't "parse" user customization files, I can put anything in there. Usually, tools just add the stuff on the end file.

CodePudding user response:

Found the way:

- awk '/^export PATH/ {$0=$0":'${COMPILE_DIR}'/go"} 1' $HOME/.bashrc > .bashrc # AWK way to append go path to the existing user "export PATH"
- sed "s_^export PATH.*_&:${COMPILE_DIR}/go/bin_" $HOME/.bashrc # SED way to append go path to the existing user "export PATH"
  • Related