sed one-liners

March 24, 2009 by

Handy one-liners for UNIX stream editor sed.

Replacing text in XSL, the dirty way

February 24, 2009 by

Here the example replaces quotes with escaped quotes \”, but can be generalized.

First define a template:

<xsl:template name="cleanQuote">
<xsl:param name="string" />
<xsl:if test="contains($string, '"')">
<xsl:value-of select="substring-before($string, '"')" /> \"
<xsl:call-template name="cleanQuote">
<xsl:with-param name="string">
<xsl:value-of select="substring-after($string, '"')" />
<xsl:if test="not(contains($string, '"'))">
<xsl:value-of select="$string" />

Then use it at the appropriate point by calling:

<xsl:call-template name="cleanQuote">
<xsl:with-param name="string">
<xsl:value-of select="text" />

Statistics Textbook Online

February 19, 2009 by

This might be useful.

How do I read a huge file line by line in Python?

February 4, 2009 by

This is taken from here and was written by rupe.

In Python, the most common way to read lines from a file is to do the following:

for line in open('myfile','r').readlines():

When this is done, however, the readlines() function loads the entire file into memory as it runs. A better approach for large files is to use the fileinput module, as follows:

import fileinput
for line in fileinput.input(['myfile']):

the fileinput.input() call reads lines sequentially, but doesn’t keep them in memory after they’ve been read.

scp file completion on remote host.

November 21, 2008 by

Next time you try to scp a file to or from a remote host, try and see if autocompletion is support in the same way it works for cp. You probably need to have ssh keys setup and an ssh agent running.

e.g. scp<press tab tab to autocomplete and see a list of time>

Using ssh forwarding to retrieve papers instead of VPN

November 5, 2008 by

So far I was using VPN to connect to my university network when I needed to download a paper from home, but that meant to temporarily lose my network connection, with all the hassle attached.

Today I discovered a much simpler method:

1) Open a ssh port forwarding to the remote machine (university server with IEEE or ACM subscription) in this way:

ssh -D 8080 -N <username>@<server address> &

where -D indicated the local port to forward, and -N avoid opening a shell.

2) Download the FoxyProxy Firefox extension, and configure in order to use a proxy on localhost:8080.

3) Add rules to FoxyProxy so that the proxy is active only when needed (*.ieee.*, *.acm.* etc…).

Thanks to Timo Reimann for having suggested that.

Setting up Edimax EW-7318USg under Linux

November 2, 2008 by

This is from here:

Edimax EW-7318USg aka Hawking HWUG1 (rebranded in US)

# Driver : rt73
# Chipset : Ralink
Ralink RT2571W chipset (can be used with rt73 drivers)
chipset : RT2571WF or RT2528L

HOWTO:RT2500 wireless cards etc. sticky
Ralink 257x/2671 using RT73 driver?

I think that you might be doing some of these steps in the wrong order. eg from the instructions I was given you need to blacklist first. Here are the instructions I use:

1. sudo rmmod rt73usb (remove old drivers)

2. sudo gedit /etc/modprobe.d/blacklist and add these lines to the end of the file:

blacklist rt73usb
blacklist rt2570
blacklist rt2x00lib
blacklist rt2500usb
blacklist rt2x00usb

3. sudo apt-get install build-essential

4. sudo apt-get install linux-headers-`uname –r`

5. get the latest version of the driver source from the serialmonkey site. The name is rt73-cvs-daily.tar.gz. I saved it in my user dir:

6. sudo wget -O ~/rt73-cvs-daily.tar.gz

7. sudo tar -zxvf rt73-cvs-daily.tar.gz

8. cd ~/rt73*/Module

9. sudo make

10. if the file produced is 2Mb in size there is a problem as it should be about 250Kb. To fix this, use the “strip” command:
strip –S rt73.ko

11. sudo make install

12. sudo modprobe rt73

13. as sudo, edit /etc/modules – add the text rt73 at the end

14. as sudo, create text file called rt73 in /etc/modprobe.d

15. put the text “alias wlan0 rt73” in this file

16. remove /etc/modprobe.conf as it’s no longer needed (back it up first – but note I didn’t have one)

17. add the following to /etc/network/interfaces file. You might need to customise this to suit your particular situation (eg if you don’t use WPA encryption):

auto wlan0
iface wlan0 inet dhcp
pre-up ifconfig wlan0 up
pre-up ifconfig wlan0 down
pre-up ifconfig wlan0 up
pre-up iwconfig wlan0 essid “yourSSID” # use quotes if you have spaces in the name of essid
pre-up iwconfig wlan0 mode Managed
pre-up iwpriv wlan0 set Channel=11 # change channel accordingly
pre-up iwpriv wlan0 set AuthMode=WPAPSK
pre-up iwpriv wlan0 set EncrypType=TKIP
pre-up iwpriv wlan0 set WPAPSK=”yourkey”
pre-up ifconfig wlan0 up

18. reboot

making a bootable windows iso from a local filesystem

October 25, 2008 by

mkisofs -b <boot.img> -no-emul-boot -boot-load-seg 1984
-boot-load-size 4 -iso-level 2 -J -joliet-long -l -D
-relaxed-filenames -N -V WinXP -v -x .DS_Store -o <windows.iso> <inputDir>

where <boot.img> is the location of the .img file for windows
<windows.iso> is the location and name of the output and <inputDir> is
teh location of the input filesystem

Add the bibliography in the Table of Contents

October 5, 2008 by

From here:

You have to manually add the “Bibliography” entry into the table of contents. In order to do so, put the following just before the \bibliography entry


if you use the book or report document class, or


if you use the article document class.

In combination with the “Hyperref”-Package you need to put the line below the \bibliography entry.

How to list all file extensions within a directory

October 1, 2008 by

Here’s a simple command to list all file extensions (recursively) within the current directory
(thanks to this forum posting

find . -type f | sed -e 's/.*\.//' | sort | uniq -c | sort -rn

When you have, for example, a large codebase with lots of files that have no extension (e.g., Makefile, README, etc.), the above lists them with their full path.

Here’s a modified version of this to remove any /path/to/file/ and so just list those files too:

find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort | uniq