bash - sed or awk remove multiple lines starting with `if` and ending with that `if`'s closing `fi` -


an extension question delete multiple lines - "patterna" match, through second occurrence of "patternb" ... thinking should make more robust.

so, rather counting how many fi's, in likelihood there may unknown amount, i'd able execute where...

if have following file /somepath/somefile containing:

...  # test something? if something if somethingelse somethingelse fi fi  ... 

...with unknown amount of possible if/fi statements, how can remove line starting string containing "something?" through line containing closing "fi" first if? any/all appreciated.

generally speaking, need parser this. however, if of commands in script follow pattern sample in question (if/fi @ beginning of line), crude if-counting solution should work.

awk 'begin { del=0; level=0 } /test something?/ { del=1 } del==0 { print } /^if / { level++ } /^fi( |$)/ { level--; if (level == 0) del=0 }' somefile 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -