Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating the Alphabet

Status
Not open for further replies.

chipperMDW

Programmer
Mar 24, 2002
1,268
US
Occasionally, I'll need to write a script that does something for each letter in the alphabet. I used do something like:
Code:
for x in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
    echo $x
done

That's tedious to type, and it always seemed like there should be a better way, like a [tt]seq[/tt] for letters. I recently discovered a tool called [tt]jot[/tt]. With it, I can do the following:
Code:
for x in $([highlight]jot -c 26 a[/highlight]); do  # Generate 26 characters starting at ASCII 'a'
    echo $x
done

I was curious about what other methods people had for accomplishing the same task. I'm sure there are some nifty perl and awk one-liners.

I'm especially curious to see if there's a way to do this that doesn't involve a dependence on ASCII codes and works with POSIX [tt][:alpha:][/tt]-style character classes.
 
for x in $(awk 'BEGIN{for(i=97;i<=122;++i)printf "%c\n",i}'); do # Generate 26 characters starting at ASCII 'a'
echo $x
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Put all yours functions in one file 'functions.ksh'

--- function.sh ---
alphabet () {
for x in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
echo $x
done
}



In your main script, source the functions file :


--- Main script ---
# source functions definitions
. functions.sh
# now all functions are available



Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
You can also build a function once and use it many times:

Code:
!#/bin/ksh

function print_alpha {
echo "a b c d e f g h i j k l m n o p q r s t u v w x y z"
}

for x in $(print_alpha); do
   echo $x
done

 
Code:
for dec in $(seq 65 90)
do 
    oct=$(printf "\%o " $dec)
    echo -e $oct
done
Don't know how portable it is, and how to condense it further.

According to man seq, seq -f "%03o" 65 77 should produce octal numbers itself. It doesn't! Crap!

printf "%c\n" 65 doesn't print an A, but 6. Crap too!

:) Or am I wrong?

seeking a job as java-programmer in Berlin:
 
stefanwagner said:
According to man seq, seq -f "%03o" 65 77 should produce octal numbers itself. It doesn't! Crap!

printf "%c\n" 65 doesn't print an A, but 6. Crap too!

:) Or am I wrong?
I'm not sure. I saw the [tt]-f[/tt] flag to [tt]seq[/tt], but my manual says:
Code:
[b]-f, --format[/b]=[u]FORMAT[/u]
      use printf style floating-point FORMAT (default: %g)
and my info manual says
Code:
`-f FORMAT'
`--format=FORMAT'
     Print all numbers using FORMAT; default `%g'.  FORMAT must contain
     exactly one of the floating point output formats `%e', `%f', or
     `%g'.
Not sure why it should be limited to those format values, though. Maybe that's a bug.
 
Hm.
My german manpage does not mention floating points at all.
Poor translation!
Calling the english manpage as root gives the hint.

Code:
printf "%c\n", 65
just prints an additional comma:
6
,

seeking a job as java-programmer in Berlin:
 
printf man page (AIX53) states:

c
Accepts a value as a string and prints the first character
in the string.

You get a '6' because '6' is the first char of the string "65"


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top