Skip to content

Instantly share code, notes, and snippets.

@ftorto
Created November 2, 2017 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftorto/c9e1e629f57398683319901a07b2fe21 to your computer and use it in GitHub Desktop.
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
# 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