Nicholas Adamou
Nicholas Adamou

Reputation: 741

Replace a boolean field in JSON with sed

I am trying to replace a boolean inside a JSON string using sed.

STRING: "should-replace":false

How would I use sed to replace the false with true?

Upvotes: 3

Views: 1571

Answers (3)

James Brown
James Brown

Reputation: 37394

In awk:

$ awk -F: '/should-replace/{$2=($2=="false"?"true":"false")}1' file
"should-replace" true

Notice: false -> true, everything else (meant to mean true) -> false.

Upvotes: 1

VIPIN KUMAR
VIPIN KUMAR

Reputation: 3127

Try this -

sed '/should-replace/s/false/true/' f

Upvotes: 5

Remko
Remko

Reputation: 359

Very straightforward:

sed "s/\"should-replace\":false/\"should-replace\":true/g"

Upvotes: 1

Related Questions