#!/bin/sh ################################ ## Get rid of a number of beginning and/or ending characters on each line ## Usage: suck [NUMBER] [-a NUMBER] [-b NUMBER] [-h] ## -a Suck away the last NUMBER of characters on each line ## -b Suck away the first NUMBER of characters on each line (default) ## -h Show help screen ## Example: ls -l |suck 16 # suck out the permsissions and number of links. ## suck 1.0, Copyright (c) 2003 by Adam Katz , GPL ################################ # no args -> help [ "y$1" = "y" ] && set -- "-help" # parse arguments while [ "y$1" != "y" ]; do # --help -h* --h* h if echo $1|grep -qE "^--*h"; then # cygwin can't deal with `which $0` if $0 is a full path me=`which $0 2>/dev/null`; [ "x$me" = "x" ] && me="$0" grep "^\#\# " $me|sed -e 's/^...//' exit 0 # number elif echo $1|grep -q "^[0-9]*$"; then before="s/^.\{$1\}//" # -b* --b* -B* --B* with a space before number elif echo $1|grep -qEi "^--*b[^0-9]*$"; then if echo $2|grep -q "^[0-9]*$" then before="s/^.\{$2\}//" else echo "$0: -b value \`$2\' not a number"|sed 's:.*/::g' 1>&2 fi shift # -b* --b* -B* --B* ending in number elif echo $1|grep -qEi "^--*b.*[0-9]$"; then before="s/^.\{`echo $1|sed -r -e "s/^--*b[^0-9]*//i"`\}//" # -a* --a* -A* --A* with a space before a number elif echo $1|grep -qEi "^--*a[^0-9]*$"; then if echo $2|grep -q "^[0-9]*$" then after="s/.\{$2\}$//" else echo "$0: -a value \`$2\' not a number"|sed 's:.*/::g' 1>&2 fi shift # -a* --a* -A* --A* ending in number elif echo $1|grep -qEi "^--*a[0-9]"; then after="s/.\{`echo $1|sed -r -e "s/^--*a//i"`\}$//" # file or sed arg (sed conveniently does not have -a, -b, -A, or -B) else break fi shift done # bash magically hands the instream to sed if [ "a$before" = "a" ]; then sed -e "$after" $@ elif [ "a$after" = "a" ]; then sed -e "$before" $@ else sed -e "$before" -e "$after" $@ fi