Jobine23
Jobine23

Reputation: 130

How can I use sed to perform this replacement?

I need to replace a line in file with something

#Banner none

to

Banner /etc/motd

I tried:

sed -i "s/^#Banner none/Banner /etc/motd/" /root/testfile.sh

which is not working. Do you know how I can do what I want?

Upvotes: 3

Views: 134

Answers (2)

SomeGuy
SomeGuy

Reputation: 163

You'll need to escape your slash or use a different separator than "/".

Could use

sed -i "s/^#Banner none/Banner \/etc\/motd/" /root/testfile.sh

or

sed -i "s,^#Banner none,Banner /etc/motd," /root/testfile.sh

Upvotes: 2

clemens
clemens

Reputation: 17722

Since you have slashes in the replacement, you should use other separators than slashes, e.g. commas:

sed -i "s,^#Banner none,Banner /etc/motd/," /root/testfile.sh

Otherwise, you'd have to escape the slashes in the path:

sed -i "s/^#Banner none/Banner \/etc\/motd/"

Upvotes: 3

Related Questions