Get letter from number and assign to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get letter from number and assign to a variable
# 1  
Old 10-17-2011
Get letter from number and assign to a variable

Hi to all in forum,

I'm trying to convert the letter number between 1 (A) and 26 (Z), that part is working, my issue is how to assign the printf output to a variable:
Code:
LetterNumber=10
printf "\x$(printf %x $((${LetterNumber}+64)))"
$ J

#The problem, how to assign printf output (J in this case) to a variable?
LetterNumber=10
    set Letter=printf "\x$(printf %x $((${LetterNumber}+64)))"
echo $Letter
$

I've tried with part in blue, but I get a blank output.

Any help would be very appreciated

PS: Any optimatization of the code I have will be welcome, because It seems a little slow command I have.

Grettings
# 2  
Old 10-17-2011
On Linux use the -v option to assign printf output to a variable.

Code:
LetterNumber=10
printf -v variable "\x$(printf %x $((${LetterNumber}+64)))"
echo ${variable}

Regards,
SRG
# 3  
Old 10-17-2011
Or as perl one liner:

Code:
perl -e '$number=3; @hash{1 .. 26} = ('A' .. 'Z');print "letter= $hash{${number}}\n";'

As a perl script:

Code:
#!/usr/bin/perl -w
use strict;
my $number="26";
my %hash;
@hash{1 .. 26} = ('A' .. 'Z');
print "letter= $hash{${number}}\n";

Regards,
SRG

Last edited by Paragon1970; 10-17-2011 at 10:39 PM..
# 4  
Old 10-17-2011
Hi SRG,

Correct!!, it works. Many thanks.

Only to use the variable content I need to use it in a for loop,
but is not working in this way.
Code:
LetterNumber=10
    printf -v letter "\x$(printf %x $((${LetterNumber}+64)))"

for i in {A..${letter}}; do
  echo $i
done

the for loop should be from A to J, but instead of get the A,B,C,D,E,F,G,H,I,J, I get {A..J}.

How would be the correct syntax to use variable letter within for loop?

Thanks for help so far.

Regards

Last edited by Ophiuchus; 10-17-2011 at 10:09 PM..
# 5  
Old 10-17-2011
You could try it this way....

Code:
LetterNumber=10
for i in $(seq 1 ${LetterNumber})
do    
   printf -v letter "\x$(printf %x $((${LetterNumber}+64)))"
   echo ${letter}
done

Personally, I would more than likely use perl as it is more cross platform friendly. For example the -v switch does not appear to be available in Solaris10 bash printf command, which in itself is a little weird as printf is internal to bash, so I would have thought it should be pretty much the same across bash instances on various OS but hey ho....

Regards,
SRG

Last edited by Paragon1970; 10-17-2011 at 11:24 PM..
# 6  
Old 10-17-2011
Thanks SRG,

I'm trying as below but I receive syntactix error, I'm not sure why:
Code:
LetterNumber=10
for i in $seq(1 ${LetterNumber})
do    
   printf -v letter "\x$(printf %x $((${i}+64)))"
   echo ${letter}
done

# 7  
Old 10-17-2011
Ophiuchus,

I had a typo $seq( should be $(seq
I have updated my original post with the change.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Assign number of records to a variable

How does one assign a variable, x to equal the number of records in a different file. I have a simple command such as below: awk -F "\t" '(NR>5) { if(($x == "0/0")) { print $0} }' a.txt > a1.txt but I want x to equal the number of records in a different file, b.txt (10 Replies)
Discussion started by: Geneanalyst
10 Replies

3. Shell Programming and Scripting

Assign large number of blanks to a variable

I want to assign large number of blanks to a variable in Korn shell. If it is a small number it is fine like if I want to assign 3 blanks I would code var=" " But if it is a big number say 100 blanks, what is a better way? Ultimately I will use it in printf statement printf... (3 Replies)
Discussion started by: Soham
3 Replies

4. Shell Programming and Scripting

Assign a variable to number of lines in a file

Hello Gurus, Here is my requirement. I need to find the number of lines in a file and need to assign it to a variable. This is what I did and not wroking. #!/bin/ksh set -xv Src_Path=/mac/dev/Generic/SrcFiles Src_Count=wc -l ${Src_Path}/FILE_JUNE.txt Count_file = $Src_Count | awk -F... (2 Replies)
Discussion started by: thummi9090
2 Replies

5. Shell Programming and Scripting

Setting letter as variable

I need to pass letter like "c" for "-copy" and need to read by variable $sel_mod, My I know the good way of writing the script. Example: Select one of the Clone Mode ( '-copy', '-differential', '-precopy', '-nopcopy') Type the Clone mode : c Thanks in advance, Ashan ... (6 Replies)
Discussion started by: ashanabey
6 Replies

6. UNIX for Dummies Questions & Answers

Assigning a number to a letter without printing it

Hello Unix.com ! Newbie question. With awk, i can we give a particular letter a number without printing the number? input: X|X A|X A|A X|A If field is "A", then it counts as 2 If field is "X", then it counts as 3 Multiply the 2 first field and give the result in the 3th field, but... (7 Replies)
Discussion started by: beca123456
7 Replies

7. Shell Programming and Scripting

delete all characters that aren't a letter or number

hey :) if i have a variable that is example=lewisdenny(copywrite symbol) so its not a nomal letter but a symbol, how can i remove everything in the varible that isnt letter or number thanks :) and as a side little question do you know how to remove .zip from a file like if i ls... (7 Replies)
Discussion started by: lewisdenny
7 Replies

8. Shell Programming and Scripting

How to assign a shell variable to a NUMBER parameter in pl/sql?

I have the below script running for generating file from PL/SQL stored procedure. I need to declare a shell variable and then pass this to sqlplus command to pass the same as a INPUT parameter for the stored procedure. Please help to do this. #!/bin/sh minlimit=0 maxlimit=10 size=100 while... (0 Replies)
Discussion started by: vel4ever
0 Replies

9. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

10. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies
Login or Register to Ask a Question