Hi All, PHV
I am stuck with the following or if there is a better solution please post.
What I am trying to achive is to read a bin file with "od"
0000000 4837 7470 722b 3a30 3964 7331 3753 4654
0000020 3030 3030 6161 6161 6161 6161 6161 6161
0000040 6161 6161 6161 6161 6161 3781 0000 0000
0000060 0123 0108 92aa aa20 2020 2020 2020 2020
0000100 2020 2020 2020 2020 0604 1513 5516 0123
0000120 2621 96aa aaaa aaaa aaaa aaaa aaaa aaaa
0000140 aaaa aa00 0004 2020 2053 5350 0000 26ac
0000160 0000 0037 8100 0000 0001 2301 0892 aaaa
0000200 2020 2020 2020 2020 2020 2020 2020 2020
0000220 2006 0415 1355 4201 2326 2196 aaaa aaaa
0000240 aaaa aaaa aaaa aaaa aaaa aaaa 0000 0420
0000260 2020 5353 5000 0026 ac00 0000 5476 7470
0000300 722b 3a30 3964 7331 3753 4654 3030 3030
0000320 3030 3032 3030 3030 3030 3030 3030 3030
0000340 3030 3030 3030 3030 3230 3030 3030 3030
0000360 3030 3030 3030 3030 3030 3030 3032 3030
0000400 3030 3030 3030 3030 3030 3030 3030 3030
0000420 3030 3230 3030 3030 3030 3030 3030 3030
0000440 3030 3030
Result
=======
H 7 tpr+:09 ds17 VAL 0000
aaaaaaaa aaaaaa aaaa aaaa
7 80 xxxxxxxxxxaaaa 20060415135516 0123262196aaaaaaaa 000004 007599 S SP 000026 ac000000
7 80 0218552997aaaa 20060415214645 0860007249aaaaaaaa 000066 009703 S SP 000026 ac000000
T v tpr+:09 df17 VAL
Script
awk 'BEGIN{
HEADER=0;
RECORDS=-1;
TRAILER=0;
String1="";
cmd = "od -x FILENAME";
while (cmd | getline >0)
for (i=1; i<=NF; ++i)
if(length($i)>4)
{
continue
}
else
{
if (HEADER < 21)
{
HEADER=HEADER+1;
#cmd1=hex2ascii(substr($i,1,2)) " | cut -f2 -d\" \""
#cmd2=hex2ascii(substr($i,3,2))
#cmd1 | getline b ; close(cmd1);
#print b
}
else if (RECORDS<38)
#else if (RECORDS<24)
{
RECORDS=RECORDS+1;
#if (RECORDS < 23 )
if (RECORDS < 37 )
{
#RECORDS=RECORDS+1;
printf $i
}
else
{
if(substr($i,1,2)==37)
{
print ""
printf $i
RECORDS=0;
}
else
{
TRAILER=1;
RECORDS=38;
#RECORDS=25;
print""
print $i
#exit 99;
}
}
}
else
{
print $i
}
}
close (cmd)
}
###################################################
function hex2ascii(hex_string)
###################################################
{
#Convert any size hex string to 8-bit ASCII
result_ascii = "";
module = length(hex_string) % 2
if ( module==1 )
strcol="0"hex_string
strcol = length(hex_string) - 1;
while (strcol > 0)
{
#Capture binary 8-bit chunks starting from right to left
chunk = substr(hex_string, strcol, 2);
if ( chunk < 20 ){
print hex_string" ""is not a valid Hexa value to Convert to ASCII or is not printable\n"
exit 1
}
#Convert hex to decimal
dec = hex2uint(chunk);
#Convert decimal to ASCII character
digit = sprintf("%c", dec);
#Concatenate and resume looping
result_ascii = digit result_ascii;
strcol = strcol - 2;
}
return(result_ascii);
}
###################################################
function hex2uint(hex_string){
###################################################
# Convert hex string to unsigned integer
result = 0;
power = 0;
MSD = substr (hex_string, 1, 1);
for (strpos = length(hex_string); strpos > 0; strpos--)
{
digit = substr(hex_string, strpos, 1);
if (match(digit, /[a-fA-F]/))
{
gsub(/[aA]/, "10", digit);
gsub(/[bB]/, "11", digit);
gsub(/[cC]/, "12", digit);
gsub(/[dD]/, "13", digit);
gsub(/[eE]/, "14", digit);
gsub(/[fF]/, "15", digit);
}
result = result + digit*(16**power);
power++;
}
return (result);
}'
My script seems to be going nowhere
Please help
Many Thanks
Chris
I am stuck with the following or if there is a better solution please post.
What I am trying to achive is to read a bin file with "od"
0000000 4837 7470 722b 3a30 3964 7331 3753 4654
0000020 3030 3030 6161 6161 6161 6161 6161 6161
0000040 6161 6161 6161 6161 6161 3781 0000 0000
0000060 0123 0108 92aa aa20 2020 2020 2020 2020
0000100 2020 2020 2020 2020 0604 1513 5516 0123
0000120 2621 96aa aaaa aaaa aaaa aaaa aaaa aaaa
0000140 aaaa aa00 0004 2020 2053 5350 0000 26ac
0000160 0000 0037 8100 0000 0001 2301 0892 aaaa
0000200 2020 2020 2020 2020 2020 2020 2020 2020
0000220 2006 0415 1355 4201 2326 2196 aaaa aaaa
0000240 aaaa aaaa aaaa aaaa aaaa aaaa 0000 0420
0000260 2020 5353 5000 0026 ac00 0000 5476 7470
0000300 722b 3a30 3964 7331 3753 4654 3030 3030
0000320 3030 3032 3030 3030 3030 3030 3030 3030
0000340 3030 3030 3030 3030 3230 3030 3030 3030
0000360 3030 3030 3030 3030 3030 3030 3032 3030
0000400 3030 3030 3030 3030 3030 3030 3030 3030
0000420 3030 3230 3030 3030 3030 3030 3030 3030
0000440 3030 3030
Result
=======
H 7 tpr+:09 ds17 VAL 0000
aaaaaaaa aaaaaa aaaa aaaa
7 80 xxxxxxxxxxaaaa 20060415135516 0123262196aaaaaaaa 000004 007599 S SP 000026 ac000000
7 80 0218552997aaaa 20060415214645 0860007249aaaaaaaa 000066 009703 S SP 000026 ac000000
T v tpr+:09 df17 VAL
Script
awk 'BEGIN{
HEADER=0;
RECORDS=-1;
TRAILER=0;
String1="";
cmd = "od -x FILENAME";
while (cmd | getline >0)
for (i=1; i<=NF; ++i)
if(length($i)>4)
{
continue
}
else
{
if (HEADER < 21)
{
HEADER=HEADER+1;
#cmd1=hex2ascii(substr($i,1,2)) " | cut -f2 -d\" \""
#cmd2=hex2ascii(substr($i,3,2))
#cmd1 | getline b ; close(cmd1);
#print b
}
else if (RECORDS<38)
#else if (RECORDS<24)
{
RECORDS=RECORDS+1;
#if (RECORDS < 23 )
if (RECORDS < 37 )
{
#RECORDS=RECORDS+1;
printf $i
}
else
{
if(substr($i,1,2)==37)
{
print ""
printf $i
RECORDS=0;
}
else
{
TRAILER=1;
RECORDS=38;
#RECORDS=25;
print""
print $i
#exit 99;
}
}
}
else
{
print $i
}
}
close (cmd)
}
###################################################
function hex2ascii(hex_string)
###################################################
{
#Convert any size hex string to 8-bit ASCII
result_ascii = "";
module = length(hex_string) % 2
if ( module==1 )
strcol="0"hex_string
strcol = length(hex_string) - 1;
while (strcol > 0)
{
#Capture binary 8-bit chunks starting from right to left
chunk = substr(hex_string, strcol, 2);
if ( chunk < 20 ){
print hex_string" ""is not a valid Hexa value to Convert to ASCII or is not printable\n"
exit 1
}
#Convert hex to decimal
dec = hex2uint(chunk);
#Convert decimal to ASCII character
digit = sprintf("%c", dec);
#Concatenate and resume looping
result_ascii = digit result_ascii;
strcol = strcol - 2;
}
return(result_ascii);
}
###################################################
function hex2uint(hex_string){
###################################################
# Convert hex string to unsigned integer
result = 0;
power = 0;
MSD = substr (hex_string, 1, 1);
for (strpos = length(hex_string); strpos > 0; strpos--)
{
digit = substr(hex_string, strpos, 1);
if (match(digit, /[a-fA-F]/))
{
gsub(/[aA]/, "10", digit);
gsub(/[bB]/, "11", digit);
gsub(/[cC]/, "12", digit);
gsub(/[dD]/, "13", digit);
gsub(/[eE]/, "14", digit);
gsub(/[fF]/, "15", digit);
}
result = result + digit*(16**power);
power++;
}
return (result);
}'
My script seems to be going nowhere
Please help
Many Thanks
Chris