AWK: the substr command to select a substring

Under Linux, the awk command has quite a few useful functions. One of them, which is called substr, can be used to select a substring from the input.
Here is its syntax:

substr(s, a, b) : it returns b number of chars from string s, starting at position a. The parameter b is optional, in which case it means up to the end of the string.

For instance, consider a file with the following content:

every good

Let us save this data into a file called data.txt
Then, here are a few case examples:

awk '{print substr($1,1,1)}' data.txt   #returns e
awk '{print substr($1,3)   }' data.txt   #returns ery
awk '{print substr($2,3)   }' data.txt   #returns od
awk '{print substr($0,7,2) }' data.txt  #returns go
Posted in Linux | Tagged | 12 Comments

Python: string substitution using dictionaries

A nice way to populate a string with values is to use a dictionary, which can be defined outside of the print statement.

The standard way is to write

>>> print "The temperature in %s is %f degrees" % ('celsius', 20)
"The temperature in celsius is 20 degrees"

Another way using dictionaries is as follows:

>>> data = {'unit': 'celsius', 'value':20}
>>> print "The temperature in %(unit)s is %(value)f degrees" % data
"The temperature in celsius is 20 degrees"

This is very convenient when you want to rephrase a sentence without changing the data structure:

>>> print "The temperature is %(value)f degrees (%(unit)s)." % data
"The temperature is 20 degrees (celsius)."
Posted in Python | Tagged | 1 Comment

How to install a local WordPress (Linux Ubuntu)

This blog (in French) provides an article that explain how to install a local version of WordPress (Linux Ubuntu) (Le Blog de Lise).

Posted in Computer Science, Linux | Tagged | Leave a comment

SED: how to add a line at the top of files

Imagine, you have a set of ReST files, and you want to add the statement “author: your name” at the top of each of them. First, identify the files you want to edit and pipe them to sed:

ls *.rst | xargs  sed  -e '1i author: your name'

Where ‘1i’ means insert on the first line. Note also that you must replace sed -e by sed -i -e to replace the file content, otherwise it is like a dry run.

Posted in Linux | Tagged | Leave a comment

Missing package in rpy2 (ggplot2)

When using rpy2, the python implementation of R, I quickly came up with the issue of having missing packages (e.g., ggplot2)

With a fresh installation of rpy2 (and R-core), if you type the following code in your python shell:

from rpy2.robjects.packages import importr
importr("ggplot2")

You may have the following error message


Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called 'ggplot2'
Error in .Primitive("as.environment")("package:ggplot2") :
no item called "package:ggplot2" on the search list

The solution is to install the missing package. This is not a Python package (so no use to try with easy_install). If you want to install a new R package, a compilation process will be required, so you must have install the R header files. For instance under Fedora,

sudo yum install R-devel

Then, start R by typing “R” in a unix shell. R should start and prompt a command line where you just type:


install.packages(c("getopt","ggplot2"),dependencies=TRUE)

This command will download the relevant R packages, compile them and install them on your system. It may be longish depeding on the dependencies. Finally, restart your python shell and you should now be able to use the freshly installed packages.

Posted in Computer Science | Tagged , | 3 Comments

Fedora: how to add a sudo user (and avoid password)

How to add a user in the sudo list

If you need to type commands using sudo, you will soon be tired to enter the sudo password again and again. Here are the steps to add a user in the sudo user list and to avoid typing the password. note that those commands need to be run with sudo.

First, edit the file /etc/sudoers, and add the following line somewhere:


%users ALL=(ALL) ALL

Save and exit. Then, you need to add your name to the user group. Type the following command in a shell:

usermod -a -G users <your username>

From now, you can use the command sudo from your account.

How to automatically login as sudo user without password

However, you will still need to enter the password. So, to avoid this annoying requirement, you can edit /etc/sudoers and add this command at the end of the file:


%admin ALL=(ALL) NOPASSWD: ALL

Save and exit. Finally, you will also need to create the “admin group”:


groupadd admin

and again, you need to be part of the admin group. So type this command in a shell:

usermod -a -G admin <your username>

You should be able to use sudo without password from now on.

Posted in Linux | Tagged , | 3 Comments

Fedora yum update fails

Sometimes, when you want to update your Fedora packages with :
yum update

the process fails with an error message similar to:


Error: Cannot retrieve repository metadata (repomd.xml) for repository:
rpmfusion-free-updates. Please verify its path and try again

The first solution (permanent) is to change your yum repository file in /etc/yum.repo.d .

A second (temporary) solution is to use the yum option –disablerepo. To solve the issue related to the example above, type


sudo yum --disablerepo=rpmfusion-free-updates --disablerepo=rpmfusion-nonfree-updates --disablerepo=rpmfusion-free update

Posted in Linux | Tagged , | Leave a comment

Fedora 14, slow wireless

I’ve recently posted a link related to Slow network under Fedora 14 distribution. The solution provided is related to iptables and is still working (on my distribution at least). However, I’ve also noticed that the wireless remains very slow while a wire connection works well. Many Fedora users seem to have the same issue.

It has been quite difficult but I finally figured out a solution to get back a fast (normal) wireless connection. The main idea is to play with the mode, as suggested on some forums. I won’t explain what is the mode. You can figure out what is the mode y typing the iwconfig in a shell. Information is printed on the screen. It looks like:


wlan0 IEEE 802.11bg ESSID:"*******"
Mode:Managed Frequency:2.452 GHz Access Point: 00:1E:37:11:5B:63
Bit Rate=54 Mb/s Tx-Power=20 dBm
Retry long limit:7 RTS thr:off Fragment thr:off
Power Management:on
Link Quality=68/70 Signal level=-42 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0

the first line mention the mode: 802.11bg. In this case the mode is bg. Some people suggested that the mode of your internet box must be changed to b-mode only. So, I connected myself to the Livebox by typing


192.168.1.1

in Firefox. A menu appears requesting a login/password. Then, you can go to the wireless submenu. There, an option allows to switch the mode from bg to b only.

It did not seem to solve the issue but I kept this configuration.

Then, I went to the Fedora System menu ->preferences->network connections. There, I select the wireless tab, which options can be edited. Now you have a mode option (set to infrastructure by default). I set it to adhoc. Save and restart the network. Restart Firefox and it works for me !

Now, the question is “was the provider box step required ? “. I should get it back to mode bg to see if the first step was required.

Another issue: although the wireless is now much faster, there is always a short delay of about 1-2 seconds before the page opens. I think this is a DNS issue, another issue to be solved…

Posted in Linux | Tagged , | Leave a comment

Books: Snow, Orhan Pamuk

I’ve read Snow last year, a novel from Orhan Pamuk, a Turkish novelist, Nobel prize in Literature 2006. For an overview you can consult wikipedia entries in French (Neige) or English (Snow).

Posted in Uncategorized | Tagged | Leave a comment

How to create an ISO image from a Folder (linux)

Under Linux, you can create an ISO image from a folder using a command line argument with the mkisofs tools. The following example creates an ISO image suitable to be used under Windows:


mkisofs -J -l -R -V "Label CD" -iso-level 4 -o output.iso input_directory

The -J option is used to generate Joliet directory records in addition to regular ISO9660 filenames (useful when the discs are to be used on Windows machines). When using the -J, it is recommended to add the -R option (Rock Ridge protocol). Finally, the -l option manages the long names.

If you want to create an ISO image for your Linux box only, simply type:


mkisofs -o output.iso input_directory

Then, you will need to mount it. Use Daemon tools (or equivalent under Windows) and the following syntax under Linux

First, to read the ISO file:

mdconfig -a -f ./image.iso

The command mdconfig returns the relevant memory disk (let us say md0). Now, you need to mount it:


mount -t cd9660 /dev/md0 /mnt

Those two commands could be put together in one command line:

mount -t cd9660 /dev/`mdconfig -a -f ./image.iso` /mnt

Once done, don’t forget to umount the disk:

umount /mnt
mdconfig -d -u 0

Posted in Linux | Tagged | 1 Comment