need help in expanding hexa decimal character range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help in expanding hexa decimal character range
# 1  
Old 05-13-2010
need help in expanding hexa decimal character range

Hi all,

I have a input like this

Code:
3AF9:3B01

and need to expand to the below output

Code:
3AF9
3AFA
3AFB
3AFC
3AFD
3AFE
3AFF
3B00
3B01

Please let me know the easiest way for achieving this. Thanks for the help in advance...


Moderator's Comments:
Mod Comment Added code tags!

Last edited by radoulov; 05-13-2010 at 05:50 AM..
# 2  
Old 05-13-2010
Code:
perl -e'
  @range = map hex, split /:/, shift;
  printf "%X\n", $_ for $range[0] .. $range[1];
  ' 3AF9:3B01

This User Gave Thanks to radoulov For This Post:
# 3  
Old 05-13-2010
A solution with awk:
Code:
echo "3AF9:3B01" | awk '
{
  split($0,a,":")
  for(i=hex2dec(a[1]);i<=hex2dec(a[2]);i++){
    printf("%X\n", i)
  }
}
function hex2dec(h,i,x,v){
  h=tolower(h);sub(/^0x/,"",h)
  for(i=1;i<=length(h);++i){
    x=index("0123456789abcdef",substr(h,i,1))
    v=(16*v)+x-1
  }
  return v
}'

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 05-13-2010
Thanks for the replies guys...
# 5  
Old 05-13-2010
Code:
a=3AF9 b=3B01

# POSIX compliant
echo "ibase=obase=16; a=$a; b=$b; while (a<=b) a++" | bc

# Not POSIX
jot -w '%X' - 0x$a 0x$b 1

Regards,
Alister

Last edited by alister; 05-13-2010 at 06:55 PM..
# 6  
Old 05-13-2010
more shorter, need gawk.

Code:
echo "3AF9:3B01" | awk -F : ' {for (i=strtonum("0x" $1);i<=strtonum("0x" $2);i++) printf("%X\n", i)}'

# 7  
Old 05-13-2010
Using ksh93 ...
Code:
#!/bin/ksh93

x=3AF9:3B01

typeset -i16 a=0x${x%:*}
typeset -i16 b=0x${x#*:}

for (( ; a <= b; a++ ))
do
   printf "%X\n" $a
done


Last edited by fpmurphy; 05-13-2010 at 10:56 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Removing letters after a certain character within a range of columns

Hi there, I am trying to remove al letters after : character on specific columns from 10th column till 827. I used sed and cut to do so but I am sure there is better one liner someone can think of from unix community members. Huge file but it has this structure (Total number of Columns =... (10 Replies)
Discussion started by: daashti
10 Replies

2. Shell Programming and Scripting

Grep - build character range with octal or hexa representation

Hello I would like to make a character range like that : echo "ABCDEF+1234" | grep -E ''or echo "ABCDEF+1234" | grep -E ''or echo "ABCDEF+1234" | grep -E ''Which should works on linux with english language And works also on linux with french language ( english install and after add french )... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

Use decimal value of array in bc ends with illegal character

hi all I have to read a long cvs file every 4 columns with decimal "3,45" numbers. The 9th row in this cvs is the first line I need, so it I tail -n+9. I use sed -e 's/,/./g' to get decimal values with . delimiter. So far no problem. Goal is to get two maximum negative forces in ranges... (5 Replies)
Discussion started by: Grille
5 Replies

4. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

5. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

6. UNIX for Advanced & Expert Users

Finding a specific range of character in file

hi, I want to store from 102 character to 128 character to a variable of header record which can be identified as 'HDR' which is the first 3 characters in the same line of a same.txt file. Please advise. Thanks (4 Replies)
Discussion started by: techmoris
4 Replies

7. Shell Programming and Scripting

read into a range of character

i have this problem: i must hide a string with a character such as _ by command WORD=string; XXX=`echo $WORD | sed 's//_/g' but after, users must send in input a character and i must to replace the _ with the input character or better i can do this -$CHARS_INPUT i have think to use command... (3 Replies)
Discussion started by: tafazzi87
3 Replies

8. UNIX for Dummies Questions & Answers

Use of character range in awk

Hi all, I am having a bit of a hard time using awk. I must do something wrong, but I don't know what... Any help would be greatly appreciated! I read a file, as follows :... ATOM 21 C THR A 4 23.721 -26.194 1.909 1.00 32.07 C ATOM 22 O THR A 4 ... (2 Replies)
Discussion started by: hypsis
2 Replies

9. Shell Programming and Scripting

Decimal value for special character

I am seeing an special character in my file when i do the cat filename | od-bc I see a value of 376 for that special character. I would like to find the decimal value for the character. For example the decimal value for ctrl-Y is char(25). Appreciate help on this. (11 Replies)
Discussion started by: pinnacle
11 Replies

10. Shell Programming and Scripting

How To Make Decimal Point Fall On The 15th Character On The Screen

If i have a variable which is a decimal number, i.e 34.05 How can you make decimal point fall on the 15th character on the screen? Or any other that you can specify? Can you do it using sed or awk? (3 Replies)
Discussion started by: Vozx
3 Replies
Login or Register to Ask a Question