I have a big config.js file and I would like to replace default:false,
to default:true,
which is on top of field:'$scope.keepEffort'
. I tried multiple sed
command solutions but nothing seems to work.
{
default:false,
enabled:true,
field:'criticalPath',
filter:false,
filterValue:'',
id:'show-critical-path',
operator:'colorize'
},{
default:false,
enabled:true,
field:'$scope.keepEffort',
filter:false,
filterValue:'',
id:'effort-constant',
operator:'var'
},{
default:false,
enabled:true,
field:'$scope.automaticProgress',
filter:false,
filterValue:'',
id:'automatic-progress',
operator:'var'
},{
default:false,
enabled:true,
field:'groupView',
filter:false,
filterValue:'',
id:'gantt-group-view',
operator:'var'
},{
CodePudding user response:
This is a job for awk
. The following does not attempt to match the single quotes since doing so requires some shell quoting that obfuscates the solution. Also, a trailing {
is printed. That is easy enough to remove, and the code for doing so is omitted for clarity:
awk '/field:.\$scope.keepEffort/{gsub("default:false","default:true")}1' RS=\{ ORS=\{ input-file
The idea is simply to separate the records by {
and then perform the substitution (via gsub
) only on records that match the desired line.
CodePudding user response:
This might work for you (GNU sed):
sed ':a;/{/{n;:b;N;/}/!bb;/\$scope.keepEffort/s/\(default:\)false,/\1true,/;ba}' file
Gather up lines between {
and }
and if those lines contain $scope.keepEffort
replace default:false
by default:true
.
N.B. The addition of the n
after matching {
which allows the matching of }
. Also, the return to :a
after gathering a collection so as to be able to match another {
.