Archive for the ‘awk’ Category

readable IOMessage errors

Monday, April 21st, 2008

perror() has it’s uses, but sometimes it’s more convenient to just have a string translation for the global error.
awk '/#define/ {
if(match($0,"[/][*][ ]*.+[*][/]"))
{
a=substr($0,RSTART+3,RLENGTH-6)
}
else a="";
printf " case %s : return \"%s - %s\";\n",$2,$2,a;
next
};
/^#/' /usr/include/sys/errno.h

You can make something similar for IOMessages
awk '/#define/ {
printf " case %s : return \"%s\";\n",$2,$2;
next
};' '/Developer/SDKs/MacOSX10.5.sdk/System/Library/
Frameworks/IOKit.framework/Versions/A/Headers/IOMessage.h'

HTML & more

Monday, June 25th, 2007

If you’ve ever coded anything in raw HTML you likely have come across the need to escape a certain special character now and then. For example, you should never use the ampersand (&), or the greater-than (>) or less-than (<) symbols in raw html because those symbols represent special control characters. Instead you use coded character entities, sometimes called ampersand-escape sequences because they look like ampersand, followed by a code, followed by a semi-colon (see what the raw ampersand is used for?)

So, to get an ampersand you would put & There are a handful of commonly used escape sequences like this one such as ” = “, < = <, and > = >. These mnemonic escapes are nice, but they don’t cover everything. To get at the rest, you use &#NNN; where NNN is the decimal character code in the ISO-8859-1 character set …. uh, yeah whatever that means. Sometimes I think it would be easier to just look at a list and find the code for the character you want.

Here’s an awk script that generates a primitive html list of all the characters up to 1000:
awk 'BEGIN {
print "<html><body>";
for (i=0;1000>=i;i++){
printf "&#%0.3d; = &#%0.3d;<br/>\n",i,i;
}
print "</html></body>";
}' - > ampersand.html

drop ups?

Saturday, June 23rd, 2007

Here’s a slightly generalized revision of the drop down awk script (this one can go up):

awk 'BEGIN {
  t=start=60;stop=120;drop=2;
  count=(start-stop)/drop;
  if(0>count){drop=-drop;count=-count};
  while(count>=i){
    if(59<t){m=t/60;s=t%60} else {s=t;m=0};
    printf "#%2d on %1d:%02d leave on :%02d\n",++i,m,s,c;
    c=(c+s)%60;t-=drop;
  }
}' -

Drop downs

Monday, June 11th, 2007

This morning at swimming we did drop downs. Coach Whitney just got back from a week at the beach looking tan and happy, and I guess she wanted to test how we had done in her absence.

The drop down set works like this: you start with a two minute interval and swim as far as you can while still starting and stopping at a wall; then you drop the interval by two seconds and repeat this pattern until the interval reaches one minute. When you miss an interval you just drop down to the next shorter distance and get back on pace.

We did this set in a 25 yard pool, so the typical distances for most people ranged from 150 at the start to 50 at the end, with 125, 100, and 75 in between. The send-off times can get rather tricky if you aren’t a natural math whiz, so it helps to figure them out in advance. Here’s how it’s supposed to work if you start the set when the pace clock is “on the top”:

# 1 on 2:00 leave on :00
# 2 on 1:58 leave on :00
# 3 on 1:56 leave on :58
# 4 on 1:54 leave on :54
# 5 on 1:52 leave on :48
# 6 on 1:50 leave on :40
# 7 on 1:48 leave on :30
# 8 on 1:46 leave on :18
# 9 on 1:44 leave on :04
#10 on 1:42 leave on :48
#11 on 1:40 leave on :30
#12 on 1:38 leave on :10
#13 on 1:36 leave on :48
#14 on 1:34 leave on :24
#15 on 1:32 leave on :58
#16 on 1:30 leave on :30
#17 on 1:28 leave on :00
#18 on 1:26 leave on :28
#19 on 1:24 leave on :54
#20 on 1:22 leave on :18
#21 on 1:20 leave on :40
#22 on 1:18 leave on :00
#23 on 1:16 leave on :18
#24 on 1:14 leave on :34
#25 on 1:12 leave on :48
#26 on 1:10 leave on :00
#27 on 1:08 leave on :10
#28 on 1:06 leave on :18
#29 on 1:04 leave on :24
#30 on 1:02 leave on :28
#31 on 1:00 leave on :30

This morning I did 2 175s, 11 150s, 4 125s, and 13 100s, and I just made the last one on 1:00 going :59. This was only the second time that I’ve finished out the set on 100s, so I was fairly pleased. Immediately afterwards, there was a brief discussion about whether I should have done one more, but in any event, I wasn’t prepared to do another one right then so I didn’t. I think Whitney was correct that we did only 30 and not 31, but I think this happened because we skipped the first one (not the last one)!

Here’s the Awk script that I used to generate the send-off times:

awk 'BEGIN {
  t=start=120;stop=60;drop=2;
  while(stop<=t){
    if(59<t){m=t/60;s=t%60} else {s=t;m=0};
    printf "#%2d  on %1d:%02d  leave on :%02d\n",++i,m,s,c;
    c=(c+s)%60;t-=drop;
  }
}' -