mikeage.net Logo

mikeage.net @ ב׳ שבט תשע״ח

Posts Tagged ‘Tip’

PHP Breadcrumbs

‍‍י״ג אלול תשס״ו - Tuesday, September 5th, 2006

I just redid the bread crumbs code on my site to handle breadcrumbs without horribly mangling links containing slashes the query string. Details below.

Converting between DOS and Unix

‍‍ז׳ מנחם אב תשס״ה - Friday, August 12th, 2005

Quick Overview

DOS (and Windows) use different formats to mark the ned of a line in a text file. DOS uses a CR-LF, while Unix just uses a LF. There are many ways to switch between the formats.. some common ones are below

Explicit Programs

The programs dos2unix and unix2dos provide a simple way to switch between the two formats. These are also known on some systems as todos and fromdos.


tr -d '\15\32' < dosfile > unixfile


DOS to Unix
awk '{ sub("\r$", ""); print }' < dosfile > unixfile
Unix to DOS
awk 'sub("$", "\r")' < unixfile > dosfile


DOS to Unix
perl -p -e 's/\r$//' < dosfile > unixfile
Unix to DOS
perl -p -e 's/\n/\r\n/' < unixfile > dosfile


Careful of AWK Comments

‍‍ז׳ מנחם אב תשס״ה - Friday, August 12th, 2005

Passing a script with comments from a shell script.

If you're like me, you write awk scripts by creating a file, let's say script.sh, and it looks something like this (let's say.. count lines where the 2nd comma-delimited number is greater than 3):
#!/bin/sh<br />
awk -F, 'BEGIN {<br />
# Minimum threashold<br />
}<br />
{<br />
# Let's do this thang<br />
if $2 > THRESHHOLD <br />
COUNT++ <br />
} <br />
END { <br />
print "COUNT = " COUNT<br />

(Quick note: I know the IF statement can be eliminated, but I left it in so even non-AWK programmers can benefit from this example.)
Now, this code will not run. Why not? Let's try and see what happens:
~/tips/linux > ./badscript.sh < testfile<br />
awk: line 7: missing } near end of file<br />
./badscript.sh: line 10: syntax error near unexpected token `}'<br />
./badscript.sh: line 10: `} '<br />

Strange. Quite strange. Let's look at lines 7 and 10:
# Let's do this thang<br />and<br />
} <br />

One is a comment, one is just the closing brace. Count the braces-- they line up. And how is the } missing... it's on line 10 (ironically, the other error)! Think about it for a minute

Ok, here's the answer. The apostrophe in let's is really a single quote, which closes the script. Just because awk knows that # is a comment doesn't prevent the shell from getting confused. Solution: either write "let us" instead, or just pass the script as awk -f &lt;scriptfile&gt;.


Full Matrix to Half Matrix

‍‍ז׳ מנחם אב תשס״ה - Friday, August 12th, 2005

Quick Overview

How to make a directed representation of a symetric matrix into an
undirected representation.


That's a long task. Really what it means is as follows. Suppose I have a matrix, let's say, A[i,j]. Now, if A is sparse (that is, most of the elements are zero / null / any constant value), we may choose to store it in a text file like such:

Now, sometimes you have a symetric matrix, where the value is a function of i and j. For example, those distance guides you see on AAA maps are triangular, not square, because the distance between Washington and New York is the same as the distance between New York and Washington. In other words, A[i,j] = A[j,i] for all i and j.
[In case you think this is obvious, you may not understand what the matrix is for. Suppose instead of Dist[NY,Wash] = Dist[Wash,NY] we had Moving[NY,Wash] to represent the number of people moving from NY to Washington. Obviously, that number would be different from Moving[Wash,NY], which is the number of people moving from Washington to New York.]
Getting back to the task, how can we easily filter out the duplicated elements, without having to take the text file, make a giant matrix in memory, and then write out the upper (or lower) half?


We can do the following:
For each line,

  • Read
  • Write it both forwards and backwards
  • Loop
  • Then, sort, and remove duplicates (easy with the sort and uniq commands). Then, just loop through, removing all elements where i is greater than j.


    Some people may say this is obvious. Well, it is... but I didn't see it immediately. Thanks to Dr. Dick Klavans for pointing it out to me.

Bezeq PPPoA with an Alcatel Speedtouch 510

‍‍ז׳ מנחם אב תשס״ה - Friday, August 12th, 2005

Quick Overview

Bezeq is the telephone company in Israel, and the supplier for ADSL lines here. Although one can sign up for any ISP they choose, all lines come from Bezeq, and use their setup. By default, the modems they give try and use PPTP or PPPoE, requiring a dialer program running on a PC. One alternative is keeping one's PC on all day is the use of a router-- but what if someone wants to just plug the modem into a switch? The Alcatel Speedtouch 510 they sell has a full router built in-- and it would be nice if we could use it. Also, some reports suggest that PPPoA gives a faster throughput than PPPoE (I haven't verified this). Anyway, here's how.


Before we begin, let me point you to an excellent page at petri.co.il. Despite the MCSE in the title, the author of this site knows his stuff, and has some excellent resources. He's working on the reverse assumption of this page: namely, he tells you how to make your Alcatel as dumb as possible, to use a smarter router / firewall behind the box, and eliminate the modem's NATing and other features. Here, we'll do the opposite.

Who can use this?

Anyone with:

  • An Alcatel 510 speedtouch modem
  • More than 1 PC / VoIP device / other ethernet device
  • A hub, switch, or router (PPPoE, L2TP etc not required!)


First, let me say that I'm not responsible if this fries your modem or anything else really bad. It worked for me-- it might (or might not) work for you. Also note that I keep my modem with an IP of instead of the default. It's what I do. Download my user.ini, and upload it to your modem (under setup->system). Go back to basic, and enter your username. It should work. Enjoy.

Quick Map
Content +
Personal +
Archives +
Site Stuff +
RBS Weather +
Search +
Recent Images

Valid XHTML 1.1!
Screen Pge

Last Modified: September 04, 2006 @ 02:11 CST

Memory(TRUE): 2097152/2097152
Memory(FALSE): 3053056/3063040