user3003510
user3003510

Reputation: 301

Use Sed to replace first character if line contains pattern

I have a file that has lines of code that are commented out and can be uncommented to become useful. Example:

#debug=true;

Is there a sed command that would search the file for "bug=tr" or "debug" and remove the first character (#)? So it would become active as:

debug=true;

Thank you in advance for looking at this for me!

Upvotes: 3

Views: 6113

Answers (3)

user4512135
user4512135

Reputation:

Here is my version:

$ cat << EOF | sed -r 's/(#\s*)((de)?bug\s*\=\s*[tT]r(ue)?)/\2/'
> #debug=true
> # debug = true
> # bug = tr
> # debu = tue
> # buggy = tree
> EOF
debug=true
debug = true
bug = tr
# debu = tue
# buggy = tree

Upvotes: 0

choroba
choroba

Reputation: 241848

Yes, use the so called "address":

  sed -e '/bug=tr/ s/^#//'

# if this ^^^^^^^
# is true, run this ^^^^^

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

You could use the below,

sed '/bug=tr\|debug/s/^#//' file

This would search for the line which has the string bug=tr or debug. If it finds any then it would do replace the # symbol at the start with an empty string.

Example:

$ echo '#debug=true;' | sed '/bug=tr\|debug/s/^#//'
debug=true;
$ echo '#bug=true;' | sed '/bug=tr\|debug/s/^#//'
bug=true;

Upvotes: 2

Related Questions