Created
November 2, 2017 08:26
-
-
Save ftorto/c9e1e629f57398683319901a07b2fe21 to your computer and use it in GitHub Desktop.
Remove a block (like a comment block) if this one contains a defined pattern in a specific file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Remove a block beginning with $1, ending with $2 if it contains $3, in file $4 | |
# $1 and $2 are sed regexp escaped surrounded with '/' | |
# Example: remove_block_if_pattern_found '/\/\*/' '/\*\//' '@license' $f | |
function remove_block_if_pattern_found { | |
start=$1 | |
end=$2 | |
pattern=$3 | |
file=$4 | |
# Considering everything above "package ...;" as header | |
cat $file | awk ' | |
'$start' {rec="";f=1} | |
f {rec = rec $0 ORS} !f; | |
'$end' { | |
if (f && (rec !~ "'$pattern'") || !f ) | |
printf "%s",rec; f=0 | |
}' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment