Skip to main content

SED; adding # in front of matching line with various special characters [Resolved]

Okay even though I looked through a ton of posts here and elsewhere, I cannot get a # in front of the following line:

[ -n "$ID" -a "$ID" -le 200 ] && return

I tried several solutions for example:

sed -r "s/[ -n '$ID' -a '$ID' -le 200 ] \&\& return/#[ -n '$ID' -a '$ID' -le 200 ] \&\& return/g"

and

sed 's/[ -n "$ID" -a "$ID" -le 200 ]/#&/'

And several other ideas based on posts here and elsewhere, but still no result. I only get results like this:

[ -n "$ID" -a "$ID" -le 200 #[ -n '0' -a '0' -le 200 ] ] && return] && return return

and

#if [ -n "$BASH_VERSION" -o -n "$KSH_VERSION" -o -n "$ZSH_VERSION" ]; then
#  [ -x /usr/bin/id ] || return
#  ID=`/usr/bin/id -u`
#  [ -n "$ID" -a "$ID" -le 200 ] && return
#  # for bash and zsh, only if no alias is already set
#  alias vi >/dev/null 2>&1 || alias vi=vim
#fi

Maybe someone can explain how I can get a # (hashtag) for that particular line given aboven. Probably it's something simple and my best guess is that I am not escaping things correctly?

As mentioned above, I did look up various posts, but I still cannot seem to get this done...


Question Credit: Joanne
Question Reference
Asked September 21, 2019
Tags: sed
Posted Under: Unix Linux
18 views
1 Answers

You need to escape the square brackets:

sed 's/^\[ -n "$ID" -a "$ID" -le 200 \] && return$/#&/'


credit: rusty shackleford
Answered September 21, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA