March 19, 2009

process binary data with gawk

In times, there is need for gawk to process binary data, such as dumping the content of a binary file (mimicking od or xxd), or dump the content of a binary packet. The following script comes handy for this purpose.

#dumpbin.awk
#!/usr/bin/gawk -f
BEGIN{
FS="";
RS="\n";
for (i = 0; i <>
d[sprintf("%c", i)]=i;
}
}
{
for (i=1;i<=NF;i++){
printf("%02X ",d[$i]);
count++;
if (count%16==0)
printf("\n");
}
printf("0A ");
count++;
if (count%16==0)
printf("\n");
}

No comments:

Post a Comment