[ale] String editing using the shell (/bin/sh)

Michelangelo Grigni mic at mathcs.emory.edu
Thu Nov 30 17:01:31 EST 2000


> I'm trying to rename files using shell scripting (/bin/sh).
> How do I "edit" the "name?"
> 
> As examples, I'd like to be able to do:
> 1.  a "prefix substitution"  (Is there a shell feature that
>     will allow this?)  I can do a "prefix chop" using
>     ${p#w}, but how about a substitution?
> 2.  an "in the middle" substitution  (sed?)
> 3.  a "suffix substitution"  (same as #1, but on the
>     "other" end)  I can do a "suffix chop" using ${p%w},
>     but, as in (1.) how might I do a substitute?

Here is a sh script (I call it 'ren') which renames files
according to sed substitutions.  One problem: the value of
the 'sep' variable should be a control-A, but that may not
transport correctly via email (so, repair if necessary).

#!/bin/sh
# ren: sed filenames

IFS=' '
PATH=/bin:/usr/bin # gnu-less
export PATH
ls="ls -1A"
sed=sed
sh=cat
shx="sh -x"
upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ lower=abcdefghijklmnopqrstuvwxyz
action=''
mv="mv -i -" cp="cp -ip" lnh="ln" lns="ln -s" tch="touch -r --"
prog="$mv" casemap=''
err=no

# Parse options:
while getopts cmhtsluxp: c # handles -- automatically
do
   case $c in
   c) prog="$cp" ;;
   m) prog="$mv" ;;
   h) prog="$lnh" ;;
   s) prog="$lns" ;;
   t) prog="$tch" ;;
   l) casemap="y/$upper/$lower/" ;;
   u) casemap="y/$lower/$upper/" ;;
   x) sh="$shx" ;;
   p) prog="$OPTARG" ;;
   *) err=yes; break ;; # getopts just printed an error message
   esac
done
shift `expr $OPTIND - 1`

case "$#:$err" in
2:no)  ;;
*)  # sh bug: "exec cat << ..." leaves a file in /tmp
    cat 1>&2 << USAGE
usage: $0 [-xlucsh] [-p PROGRAM] [--] PATTERN REPLACEMENT
Prints to stdout a file renaming script (default command "$mv"),
using the sed PATTERN and REPLACEMENT to map from the old filename
to the new filename.  Options:

  -p PROGRAM   use PROGRAM as the command instead of "$mv"
  -c -s -h -t  use "$cp", "$lns", "$lnh", or "$tch" as the command
  -l -u        map resulting filenames to lowercase or uppercase 
  -x           really execute the commands! (using "$shx")
  --           use this in case PATTERN might start with a - (dash)

In typical usage, I run this first without the "-x" flag, just to see
what it would do.  If the results look good, then I run the same
command line again with the -x flag, to actually execute the commands.

Known bugs: Fails safely if an argument contains the Control-a character.
            Fails oddly if a filename contains a newline.
USAGE
exit ;;
esac

# sep is an unlikely character, fail safely if user used it:
sep=""
case "$1$2$prog" in
*"$sep"*)
    echo "$0: cannot handle Control-a char in an argument!" 1>&2
    exit 1;;
esac

action="s$sep$1$sep$2$sep"
sedprog="#n
h
\\$sep$1$sep!d
h
$action
$casemap
x
G
s/\n/' '/
s$sep^$sep$prog '$sep
s/$/'/
p"

# (for file in * ; do echo "$file" ; done) may be faster!
$ls | $sed "$sedprog" | $sh
--
To unsubscribe: mail majordomo at ale.org with "unsubscribe ale" in message body.





More information about the Ale mailing list