3

I want to replace a string in a file using command, but the thing is the string is such

"https": false,

I want to change it to

"https": true,

and vice versa.

is there any way to accomplish this thru command? Am developing auto script so whenever a user logins this command kicks in, I have sorted everything except this.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Harsh
  • 152
  • 1
  • 1
  • 9

1 Answers1

5

Use sed:

sed -i 's/"https": false,/"https": true,/g' /path/to/file

Here the -i flag means replace and save the file using the same name. Any occurrence of "https": false, will be replaced with "https": true,/ If this string only occurs at the start of a line, use this instead:

sed -i 's/^"https": false,/"https": true,/' /path/to/file

This substitution is executed on the specified file, where you can also use wildcards to perform it on multiple files, e.g. /path/tp/dir/* (all files in dir) or *java (all java files).

ShellFish
  • 4,351
  • 1
  • 20
  • 33
  • He also said vice versa – bkmoney May 25 '15 at 22:15
  • Strange that it works. This action will set any https to false (1st set all fasle to true so all are true, second action set true to false so any https will now false. You need a temporary state (to swap) or , select the pattern (with true or false) and change this in a sub action, so each true/false is treated only once. – NeronLeVelu May 26 '15 at 06:08
  • Actually all i used was sed -i 's/"https": false,/"https": true,/g' .config/deluge/web.conf nothing else. – Harsh May 26 '15 at 07:32
  • Don't forget to accept the answer if it worked for you, my edit was in fact faulty, should have seen that earlier... – ShellFish May 26 '15 at 10:15