mikeage.net Logo
mikeage.net/2005/08/

mikeage.net @ ו׳ כסלו תשע״ח

Archive for ‍‍ תמוז / מנחם אב תשס״ה - August, 2005

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

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

AWK

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

Perl

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

Enjoy.

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 />
THRESHHOLD=3<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;.

Enjoy.

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.

Overview

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:
i1,j1,value
i2,j2,value
i3,j3,value

etc.
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?

Solution

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.

    Note

    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.
    Enjoy.

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.

Alternative

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!)

How?

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 192.168.0.1 instead of the 10.0.0.138 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.

Nested Lists in XHTML

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

Quick Overview

If you want to nest lists in HTML, you probably did something like the following (although you may not have done the indenting)

&lt;ul&gt;<br />
&nbsp;&nbsp;&lt;li&gt;List Item 1&lt;/li&gt;<br />
&nbsp;&nbsp;&lt;ul&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Sublist Item 1&lt;/li&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Sublist Item 2&lt;/li&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Subsublist Item 1&lt;/li&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/ul&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Sublist Item 3&lt;/li&gt;<br />
&nbsp;&nbsp;&lt;/ul&gt;<br />
&nbsp;&nbsp;&lt;li&gt;List item 2&lt;/li&gt;<br />
&nbsp;&nbsp;&lt;li&gt;List Item 3&lt;/li&gt;<br />
&lt;/ul&gt;<br />

In XHTML, this is not valid (the w3 validator will complain about "document type does not allow element "ul" here; assuming missing "li" start-tag". The reason is that an nested &lt;ul&gt; belongs within an &lt;li&gt; tag. You might worry that this would put a higher level bullet on the sublist-- it doesn't. The key is you don't close the &lt;li&gt; tag from the previous entry until after the sublist finishes. Compare the following. (note that both are syntactically correct XHTML)

Code Output
&lt;ul&gt;<br />
&lt;li&gt;List Item 1<br />
&lt;ul&gt;<br />
&lt;li&gt;Sublist Item 1&lt;/li&gt;<br />
&lt;li&gt;Sublist Item 2<br />
&lt;ul&gt;<br />
&lt;li&gt;Subsublist Item 1&lt;/li&gt;<br />
&lt;/ul&gt;&lt;/li&gt;<br />
&lt;li&gt;Sublist Item 3&lt;/li&gt;<br />
&lt;/ul&gt;&lt;/li&gt;<br />
&lt;li&gt;List item 2&lt;/li&gt;<br />
&lt;li&gt;List Item 3&lt;/li&gt;<br />
&lt;/ul&gt;<br />
  • List Item 1
    • Sublist Item 1
    • Sublist Item 2
      • Subsublist Item 1
    • Sublist Item 3
  • List item 2
  • List Item 3
Versus
&lt;ul&gt;<br />
&lt;li&gt;List Item 1&lt;/li&gt;<br />

&lt;li&gt;&lt;ul&gt;<br />
&lt;li&gt;Sublist Item 1&lt;/li&gt;<br />
&lt;li&gt;Sublist Item 2&lt;/li&gt;<br />
&lt;li&gt;
&lt;ul&gt;<br />
&lt;li&gt;Subsublist Item 1&lt;/li&gt;<br />
&lt;/ul&gt;&lt;/li&gt;<br />
&lt;li&gt;Sublist Item 3&lt;/li&gt;<br />
&lt;/ul&gt;&lt;/li&gt;<br />
&lt;li&gt;List item 2&lt;/li&gt;<br />
&lt;li&gt;List Item 3&lt;/li&gt;<br />
&lt;/ul&gt;<br />

  • List Item 1
    • Sublist Item 1
    • Sublist Item 2
      • Subsublist Item 1
    • Sublist Item 3
  • List item 2
  • List Item 3

Enjoy.

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

Valid XHTML 1.1!
Printer Friendly Page
 

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

Memory(TRUE): 4194304/4194304
Memory(FALSE): 2999408/3009392