#!/usr/bin/awk -f # extract elements from XML # usage: awk -f tag.awk tag=tag.class file # BUG: May not handle weird nesting well, # because end tags do not have to have the class attribute. BEGIN { tag = "pre"; printing = 0; } # This should happen before the first line, # but after command line definitions. NR < 2 { class = ""; n = split( tag, a, "." ); if ( 1 < n ) { tag = a[1]; class = a[2]; } start = "<" tag; if ( 0 < length( class ) ) { start = "<" tag "[^>]*class *= *\"" class "\""; } } $0 ~ "<\/" tag { if ( 0 < printing ) { printing--; } } { if ( 0 < printing ) { print; } } $0 ~ start { printing++; }