pleasebekind
pleasebekind

Reputation: 17

Easiest way of removing # from a certain line in a file

How can I remove this line using scripts in bash

#net.ipv4.ip_forward = 1

Upvotes: 0

Views: 178

Answers (3)

Dmitry Kravtsov
Dmitry Kravtsov

Reputation: 123

As I understand from the question header, you need to uncomment the network setting - then the replacing action would be, with a backup:

sed -E -i.bak 's/^#(net.ipv4.ip_forward = 1)/\1/g' your-filename

The flag -E is used to enable the back-reference feature without masking the parentheses, thanks to David C. Rankin for clarification in a comment below.

Also, the dots might be masked, but not necessary in this example thought - assuming it is being applied on a /etc/sysctl.conf and without using flag -E and mode g it will be:

$ sudo sed -i.bak 's/^#\(net\.ipv4\.ip_forward = 1\)/\1/' /etc/sysctl.conf

Upvotes: 1

konsolebox
konsolebox

Reputation: 75498

sed -i '/^#net\.ipv4\.ip_forward = 1/d' file

Upvotes: 0

Christian Fritz
Christian Fritz

Reputation: 21364

cat file | grep -v "^#net.ipv4.ip_forward = 1" > file2

Upvotes: 0

Related Questions