[wplug] Breaking a word into the individual characters in a shell script

Vance Kochenderfer vkochend at nyx.net
Tue Aug 19 12:30:56 EDT 2008


"Rob Lines" <rlinesseagate at gmail.com> wrote:
> I have dealt with using awk to grab certain columns in a file and then
> work with them as variables but now I want to take a single word and
> break it up into the individual characters for example
> dog and I want to assign d, o and g to separate variables to work
> with.  I looked through the awk man page and I am just not finding how
> to interact with the x character that I pass it.

Moshe's solution is probably better, but another way to do it
would be:

  firstchar=`echo $filename|cut -c1`
  secondchar=`echo $filename|cut -c2`
  thirdchar=`echo $filename|cut -c3`
  and so on...

If you're using bash, it may be easier for you to use an array
variable and loop through the indexes rather than having seven
different named variables.

Vance Kochenderfer        |  "Get me out of these ropes and into a
vkochend at nyx.net          |   good belt of Scotch"    -Nick Danger


More information about the wplug mailing list