Operating System - HP-UX
1751981 Members
4935 Online
108784 Solutions
New Discussion юеВ

Re: Increasing ASCII values in shell variables

 
SOLVED
Go to solution
yaron1
Advisor

Increasing ASCII values in shell variables

Hi,

I have 2 questions that concerns giving ASCII values to shell variables. I need it for loop that increases the ASCII value of a variable each loop pass. I need something like that (I will use it for grep not echo):

$ for right_char in A B C D E
> do
> echo MYVAR${right_char}
> done
MYVARA
MYVARB
MYVARC
MYVARD
MYVARE

However, if I want the loop to go on on the letters from A to Z, I would need either:

1. A way to tell the for to go from A to Z, something like [A..B], is there such a construct?

2. Define right_char to hold ASCII code that I can increase with each loop pass with the simple + operator. What is the way to do that?

The shell is a POSIX shell.

Thanks for the answers.
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Increasing ASCII values in shell variables

Hi:

You could use a tiny Perl snippet:

# perl -le 'print "MYVAR$_" for (A..Z)'

Regards!

...JRF...
yaron1
Advisor

Re: Increasing ASCII values in shell variables

I actually need to use it in a grep command, not echo. I'm curious how to do it in shell. is there something equivalent to the Perl $_ for (A..Z)?

Thanks.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Increasing ASCII values in shell variables

Hi (again):

> I actually need to use it in a grep command, not echo.

If I understand correctly, you might do something like:

#!/usr/bin/sh
VARS=$(perl -le 'print "MYVAR$_" for (A..Z)')
for X in ${VARS}
do
grep ${X} file
done

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: Increasing ASCII values in shell variables

$ n=0
$ echo 'abcdefghijklmnopqrstuvwxyz' | sed -e "s/.\{${n}\}\(.\).*/\1/"
a

$ n=1
$ echo 'abcdefghijklmnopqrstuvwxyz' | sed -e "s/.\{${n}\}\(.\).*/\1/"
b

[...]

$ n=24
$ echo 'abcdefghijklmnopqrstuvwxyz' | sed -e "s/.\{${n}\}\(.\).*/\1/"
y

$ n=25
$ echo 'abcdefghijklmnopqrstuvwxyz' | sed -e "s/.\{${n}\}\(.\).*/\1/"
z


It's easier in DCL:

alp $ n = 12

alp $ write sys$output f$extract( n, 1, "abcdefghijklmnopqrstuvwxyz")
m

Or, more cryptically:

alp $ chr=""
alp $ chr[ 0, 8] = 97+ n
alp $ write sys$output chr
m

As usual, many things are possible.
Michael Steele_2
Honored Contributor

Re: Increasing ASCII values in shell variables

God, must be an MIS major. Awful.
Support Fatherhood - Stop Family Law
Dennis Handly
Acclaimed Contributor

Re: Increasing ASCII values in shell variables

I assume you can use printf(1) and printf within awk(1) with the %c format to print out chars.
You could also use an array of chars like Steven's:
set -A chars A B C D E F ...
echo $chars[0]
Bill Hassell
Honored Contributor

Re: Increasing ASCII values in shell variables

Here's a couple of all-shell methods:

For just the list shown in the original question:


#!/usr/bin/sh
for CHR 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 MYVAR$CHR
done


#################

For a numeric method:

#!/usr/bin/sh
set -A AZ 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
CNT=0
while [ $CNT -le 25 ]
do
echo MYVAR${AZ[$CNT]}
CNT=$((CNT+1))
done

In the second example, the CHR array can be referenced with an number between 0-25. Insert a dummy value before "A" to change the index to 1-26.

The grep requirement is not clear. If you are looking for all matches for MYVAR ending with any char A thru Z, this will do it on one line:

grep MYAR[A-Z] some_file


Bill Hassell, sysadmin
Viktor Balogh
Honored Contributor

Re: Increasing ASCII values in shell variables

maybe you could get use of this bash feature, I don't know what are you trying to accomplish:

# echo {1,2,3}{a,b,c}
1a 1b 1c 2a 2b 2c 3a 3b 3c
#
****
Unix operates with beer.
Dennis Handly
Acclaimed Contributor

Re: Increasing ASCII values in shell variables

You can't use printf(1) but awk works:
#!/usr/bin/ksh
(( char = 16#41 ))
while (( char <= 16#5a )); do
awk -v char=$char 'BEGIN {printf "MYVAR%c\n", char }' /dev/null
(( char += 1 ))
done

And you can put the whole loop in awk:
awk '
BEGIN{
for (i = 0; i < 26; ++i)
printf "MYVAR%c\n", i + 65
}' /dev/null