Atirag
Atirag

Reputation: 1740

Comment out one line of many files with sed

I need to comment out a line of many files on one path. The line reads

input_types = ['text']

and I need to replace it by

#input_types = ['text']

I want to do something like

sed -i 's/input_types/#input_types/g' path/to/files/*

but that would change all instances of input_types and I don't want that.

I also tried

sed -i 's/input_types = ['text']/#input_types = ['text']/g' path/to/files/*

but it didn't work

How can I change only that specific instance?

Upvotes: 0

Views: 40

Answers (1)

Ulukai
Ulukai

Reputation: 303

You last try was quite good, but two things have to be changed:

  • You use single quotes to enclose your expression, but single quotes are also part of the expression -- that gets confusing. In this case it's better to use double quotes for enclosing the expression, instead.
  • The [ ] brackets have to be escaped with backslashes: \[ \]

So, if you change the line to

sed -i "s/input_types = \['text'\]/#input_types = \['text'\]/g" /path/to/files/*

it should work.

Upvotes: 1

Related Questions