002A:002F
03E0
03E2
07AF:07B1
0212
0248
And there are some numbers without sequence (non-ranged). I am doing
another operation based on this output. But the next command will not
take a range unless I sort it out.
How can I expand this range, 002A:002F and display it something like
002A, 2B, 2C ... 2F ? instead of the range. Seems like have to do some
sorting. Any help would be appreciated.
Regards
I'm sure there'll be much more elegant solutions to follow, but maybe
something like the following to get your started?
sed -e "s/:/ /" < /tmp/test.in \
| while read lo hi; do
if [ -z $hi ]; then
echo "$lo"
else
lo="0x${lo}"
hi="0x${hi}"
while [ "$(( lo ))" -le "$(( hi ))" ]; do
printf "%04X\n" "$(( lo++ ))"
done
fi
done
For example:
jim@katie:~$ cat /tmp/test.in
002A:002F
03E0
03E2
07AF:07B1
0212
0248
jim@katie:~$ sed -e "s/:/ /" < /tmp/test.in \
> | while read lo hi; do
> if [ -z $hi ]; then
> echo "$lo"
> else
> lo="0x${lo}"
> hi="0x${hi}"
> while [ "$(( lo ))" -le "$(( hi ))" ]; do
> printf "%04X\n" "$(( lo++ ))"
> done
> fi
> done
002A
002B
002C
002D
002E
002F
03E0
03E2
07AF
07B0
07B1
0212
0248
which you could then pipe into sort, of course...
I'm pretty sure that many better ways exist, especially without the need for
eval:
perl -pe 's/^|:/$&0x/g;s/:/../;$_=(join "\n", map {$_=sprintf("%04X",$_)}
eval($_))."\n"'
Then pipe to sort to get it sorted.
Perhaps a bash function can do it to you.
To expand the range (or not range):
er(){
x=$[16#${1%:*}];y=$[16#${1#*:}];
while [ $x -le $y ];do printf %x "$[x++]";echo;done;
}
The addresses:
$ cat yourdata
002A:002F
03E0
03E2
07AF:07B1
0212
0248
The command to process data:
$ while read;do er $REPLY;done<yourdata
2a
2b
2c
2d
2e
2f
3e0
3e2
7af
7b0
7b1
212
248
$ echo "002A:002F
03E0
03E2
07AF:07B1
0212
0248" | perl
-lne'($s,$e)=split/:/;$l=length$s;$,=$";print$e?map(sprintf("%0*X",$l,$_),hex$s..hex$e):$s'
002A 002B 002C 002D 002E 002F
03E0
03E2
07AF 07B0 07B1
0212
0248
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
Thankx everyone for the quick response. I found all solutions working
good. :)