I have a file with visibility
declarations, spanning over one or more lines, that I want removed only if they are in a test
block.
i.e. input.txt:
test(
srcs = [
"test1",
],
visibility = [
"common",
],
deps = [ "deps" ],
)
test(
srcs = [
"test2",
],
visibility = [ "common" ],
)
i.e. output:
test(
srcs = [
"test1",
],
deps = [ "deps" ],
)
test(
srcs = [
"test2",
],
)
The visibility lines could be inside other blocks i.e. etc(...)
, in which case they should not be removed. i.e.:
etc(
src = [
"etc",
],
# should not be removed because it's not inside a test(...) block
visibility = [
"common",
],
)
This is what I have tried, however, this only matches visibility
blocks spanning over a single line:
#!/bin/bash
#remove_lines.sh
remove_visibility_lines () {
start_pattern="test"
end_pattern=")"
pattern_to_remove="visibility = \[.*\],"
sed "/${start_pattern}/,/${end_pattern}/{/${pattern_to_remove}/d}" "$1"
}
remove_visibility_lines $1
$ ./remove_lines.sh input.txt
I've tried several ways to get this to remove visibility spanning over multiple block, i.e. (.*?)
and (\_.*)
, but I can't seem to get it to work.
Please help?
Question is similar to:
Using sed to delete all lines between two matching patterns , however, in my case I have patterns nested inside patterns. I.e.: you only look inside the test(...)
block, and only inside those blocks you remove the visibility = [...],
blocks.