• tolower and toupper convert text in stdin to lowercase or uppercase
  • tolower_ansi and toupper_ansi convert text in stdin to lowercase or uppercase, while preserving any terminal colors
  • highlight_whitespace highlights whitespace from stdin in red
  • trim removes leading and trailing whitespace from stdin as a whole, not on each line separately
tolower() { tr '[[:upper:]]' '[[:lower:]]'; }
toupper() { tr '[[:lower:]]' '[[:upper:]]'; }
tolower_ansi() {
    awk '{s=$0;b=0;while(1){match(substr(s,b+1),/\x1b\[[^m]*m/);b=RSTART;e=RLENGTH;
        if(b||e=length(s)){printf"%s",tolower(substr(s,1,b))substr(s,b+1,e)}
        else{print tolower(s);break}s=substr(s,b+e+1)}}'
}
toupper_ansi() {
    awk '{s=$0;b=0;while(1){match(substr(s,b+1),/\x1b\[[^m]*m/);b=RSTART;e=RLENGTH;
        if(b||e=length(s)){printf"%s",toupper(substr(s,1,b))substr(s,b+1,e)}
        else{print toupper(s);break}s=substr(s,b+e+1)}}'
}
highlight_whitespace() { sed $'s/[ \t\v\f][ \t\v\f]*/\e[41m&\e[0m/g'; }
trim() {  # Remove leading and trailing whitespace of stdin
    awk 'BEGIN{b=0;t=""}
        b{l=match($0,/[^ \t\v\f][ \t\v\f]*$/);if(l>0){printf t substr($0,1,l);t=substr($0,l+1)"\n"}else{t=t$0"\n"}}
        !b{sub(/^[ \t\v\f]+/,"",$0);l=match($0,/[^ \t\v\f][ \t\v\f]*$/);if(l>0){printf substr($0,1,l);t=substr($0,l+1)"\n";b=1}}'
}