Set key with value on a line in a file, if not exists between specific lines, or set specific value
0
votes
2
answers
115
views
Given the following configuration file:
shape {
visible: true
type: rectangle
...
}
shape {
type: circle
visible: isRound === "true"
...
}
shape {
// comment: "visible" not set, default "true"
}
How to set using
bash
all the values for visible
key (property) to false
, without touching the comments, but keeping the old value as a comment?
The new content should be:
shape {
visible: false
type: rectangle
...
}
shape {
type: circle
visible: false // visible: isRound === "true"
// ideally, above, the old value is kept as a comment...
...
}
shape {
visible: false
// comment: "visible" not set, default "true"
}
The file is not just a list of shape
structures, may contain other entries too. This should be **QML** compliant.
awk
shows:
awk --version
GNU Awk 5.2.1, API 3.2
Copyright (C) 1989, 1991-2022 Free Software Foundation.
**Edit**
I think I can use this PCRE regex
:
(\s*shape\s*\{\n)(\s*)(.*?)(visible\:.*\n?)?(.*?)(\n\s*\})
with replace expression as:
$1$2visible: false // $4\n$2$5$6
or
\1\2visible: false // \4\n\2\5\6
but I need a tool to apply it. It is not perfect, still needs to not comment twice.
Asked by mike
(123 rep)
Feb 19, 2023, 03:42 PM
Last activity: Feb 26, 2023, 11:41 AM
Last activity: Feb 26, 2023, 11:41 AM