Harsh
Harsh

Reputation: 162

Command to replace string in a file

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.

Upvotes: 3

Views: 2335

Answers (1)

ShellFish
ShellFish

Reputation: 4551

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).

Upvotes: 6

Related Questions