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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

process input file...

Status
Not open for further replies.

unprophete

Technical User
Apr 9, 2010
31
DE
A text file contains hdisk's unique_ids in the following way:
hdisk0 0228B039606ZZZZZZZZZZZ
hdisk1 0228C010606ZZZZZZZZZZZ
------>123456789

1-5 is the hex value I need to convert to dec, and get finally output (last output column in the output is cut 6-9 from second column of the input file):

hdisk0 8843 0396
hdisk1 8844 0106

I did already something working but maybe someone have better or shorter way.

My code is:

$ cat /tmp/ffff
hdisk0 0228B039606ZZZZZZZZZZZ
hdisk1 0228C010606ZZZZZZZZZZZ
$ cat /tmp/ffff|while read a b;do S=`printf "%d\n" 0x$(echo $b|cut -c1-5)`;L=`echo $b|cut -c6-9`;echo $a $S $L;done
hdisk0 8843 0396
hdisk1 8844 0106
$


 
I'd have a generic awk program, say hex2dec.awk:
Code:
function hex2dec(h      ,i,x,v){
  h=tolower(h)
  for(i=1;i<=length(h);++i){
    x=index("0123456789abcdef",substr(h,i,1))
    if(!x)return"NaN"
    v=(16*v)+x-1
  }
  return sprintf("%d",v)
}
And then your code:
Code:
awk -f hex2dec.awk -e '{print $1,hex2dec(substr($2,1,5)),substr($2,6,4)}' /tmp/ffff


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code:
#!/bin/bash

while read a b; do
   printf '%s %d %s\n' $a 0x${b::5} ${b:5:4}
done < /tmp/ffff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top