#! /n/gnu/bin/gawk -f # Last edited on 2000-05-17 12:39:58 by stolfi # Reads a stream of element-factored words, writes the words # prefixed by number of "peaks" in the crust-core-mantle profile. /^ *$/{next;} /./{ # Delete dummy (empty) factors: gsub(/{[_]*}/, "", $0); gsub(/[_][_]*/, "", $0); # Map "ch" and "sh" to "C" and "S" to simplify processing: gsub(/ch/, "C", $0); gsub(/sh/, "S", $0); # Delete braces: gsub(/[{}]/, "", $0); # Use a copy of the word to count peaks: w = $0; # Delete "o" after "ch" and "sh": gsub(/Co/, "C", w); gsub(/So/, "S", w); # Double all letters: w = gensub(/(.)/, "\\1\\1", "g", w); # Mark local minima with "@": gsub(/[CSktpfech][^CSktpfech@][^CSktpfech@]*[CSktpfech]/, "@", w); gsub(/[ktpf][eh]*[^ktpfeh@][^ktpfci@]*[ic]*[ktpf]/, "@", w); # Count local minima: gsub(/[^@]/, "", w); # Restore "cs" and 'sh": gsub(/C/, "ch", $0); gsub(/S/, "sh", $0); print ((length(w)+1) "-" $0); } function error(msg) { printf "line %d: %s\n", NR, msg >> "/dev/stderr"; abort = 1; exit 1; }