From: krs@uts.amdahl.com (Kris Stephens [Hail Eris!])
Newsgroups: comp.unix.shell
Subject: Re: Extracting characters from a string.
Date: 9 Apr 92 21:57:57 GMT
Organization: Amdahl Corporation, Sunnyvale CA

In article <1992Apr8.221355.21685@ngala001.nt.com> galpf01@ngala005.NT.COM writes:
>Hello all,
>I am attempting to write a script which will rename files. I need to be able
>extract characters from a string in order to do this. 
>  For example, if I have a file with name of "abcdef" how could I strip off the
>first 2 characters for use later in the script. Should I use awk? If so, how?
>
>Thanx in advance.

I tried to send this in mail, and got a rejection from bcars567 at nt.com.

Anyhow...

In article <1992Apr8.221355.21685@ngala001.nt.com> you write:
>Hello all,
>I am attempting to write a script which will rename files. I need to be able
>extract characters from a string in order to do this. 
>  For example, if I have a file with name of "abcdef" how could I strip off the
>first 2 characters for use later in the script. Should I use awk? If so, how?

Well, you didn't say which shell you're using, so let me suggest a couple
of approaches.  ksh first, since this can be done with builtins:

	:
	# strip off chars to get a smaller piece of a var
	# ksh version
	CMD=/path/to/this/script

	# Gotta be in ksh
	if [ "$RANDOM" = "$RANDOM" ]
	then
		exec /bin/ksh $CMD "$@"
		exit 1	# exec failed!!
	fi

	typeset -L2 prefix	# The variable 'prefix' is left-justified and
				# two chars long

	for file		# process each positional parm as a filename
	do
	        prefix="$file"
	        echo "The prefix for file $file is $prefix"
	done

Using sh requires processing outside the script.  If you have just a few
filenames to process:

	:
	# strip off chars to get a smaller piece of a var
	# sh version

	for file		# process each positional parm as a filename
	do
		prefix=`expr $file : '\(..\)' \| $file`
	        echo "The prefix for file $file is $prefix"
	done

This is fine, like I said, for just a couple of files.  If you've got a
*lot* of files, it's best to pipe them through sed, one to a line:

	:
	# strip off chars to get a smaller piece of a var
	# sh version

	# write args to pipe...
	echo "$@" |

	# change the spaces to newlines...
	tr ' ' '\012' |

	# NOTE: the echo and tr, above, can be replaced by anything else
	# that yields a pipe of base filenames, one to a line.

	# sed to duplicate the first two chars onto next pipe...
	sed	-e 's/\(..\)/\1 \1/'	\
		-e '/^.$/s/\(.\)/\1 \1/' |

	# NOTE: the second sed command takes care of one-char filenames by
	# replicating them.  If all files on stdin to the sed command
	# are guaranteed to be at least 2 chars long, it won't be needed.

	# process prefixes and files...
	while read prefix file
	do
	        echo "The prefix for file $file is $prefix"
	done

Note that this last example is *also* a very good ksh implementation for
situations with many many files to be handled.  sed is good at this sort
of editing -- it'll outrun the while loop on most systems -- while using
ksh builtins or a fork/exec or expr (or whatever) once per file will
eventually bury your performance in the parent shell.

>Thanx in advance.

You're most welcome!  Hope this helped,
...Kris
-- 
Kristopher Stephens, | (408-746-6047) | krs@uts.amdahl.com | KC6DFS
Amdahl Corporation   |                |                    |
     [The opinions expressed above are mine, solely, and do not    ]
     [necessarily reflect the opinions or policies of Amdahl Corp. ]