Balázs Orbán
Balázs Orbán

Reputation: 559

Replace a character in Terminal

I have a file.

My second line begins with a '#' symbol.

Which command should I use to remove that symbol?

Also on the third line, I have to put a '#' symbol at the beginning of the line.

For example:

line 2: # url: http//192.168.1.1:8000
line 3:   url: http//example.com

when running the command, change the place of that '#'

Alternativly, is there a way to put the local IP in second line automatically? (So if I restart the router for example, the line will refresh the IP)

Upvotes: 1

Views: 1313

Answers (1)

choroba
choroba

Reputation: 242028

You can use sed:

sed -i~ '2 s/^#//' filename
  • -i~ will create a backup
  • 2 is the line number where the next command will be applied
  • s/ pattern / replacement / is a substitution. Here, we substitute with nothing.
  • ^ in a pattern matches the start of a line.

To remove the octothorpe from the second line and add it to the third one, use

sed -i~ '2 s/^#//; 3 s/^/#/' filename

Upvotes: 1

Related Questions