#!/bin/sh ## Read words from stdin, write to stdout in a column. ## ## Usage: ## See the variable named usage, below ## ## Blank lines are unchanged. ## Assumes each word is shorter than the desired width. errx() { echo "$@" >&2 exit 1 } # # Parse the command line # usage="usage: $0 "'[ -e | --email ] [ -w WIDTH ] [ file ]' TEMP=`getopt -o ew: --long email,width: -n 'No way, man' -- "$@"` if [ $? != 0 ] ; then errx "Terminating..."; fi # Note the quotes around `$TEMP': they are essential! eval set -- "$TEMP" eflag="" width=72 period_space=0 while true ; do case "$1" in -e|--email) eflag=yes ; shift ;; -w|--width) width=$2 ; shift 2;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done #echo "Remaining arguments:" #for arg do echo '--> '"\`$arg'" ; done n=$width [ "$n" -gt 0 ] || errx "$usage" infile="" if [ $# -gt 0 ] then infile=$1 if [ ! -r "$infile" ] then errx "not readable: $infile" fi fi cat $infile | { ## start of filter # # Filter the input # putline() { line="${line%% }" echo "${line## }" line="" } line="" #set -x while read input do # handle email headers # if [ -n "$eflag" ] then case "$input" in Date:*) # remove the time echo "$input" | sed 's/[0-9][0-9]:..:[0-9][0-9].*//' continue ;; Subject:*|From:*) echo "$input" continue ;; esac fi # preserve blank lines # if [ -z "$input" ] then if [ -n "$line" ] then #echo "$line" #line="" putline fi echo continue fi ## ## For each space-separated word in $input ... ## #eval set -- $(echo "$input" | sed 's/[^ ][^ ]*/"&"/g') set -f # no filename expansion set -- $input set +f for x do : x=":$x:" if [ "$period_space" == 1 ] then case "$x" in *.) x="$x " ;; ## extra space after period esac fi if (( ${#line}+${#x}>=n )) then putline line="$x" else line="$line $x" #echo "line:'$line'" >&2 fi done done [ -n "$line" ] && putline } ## end of filter