sed

Even to Odd converter

Here is an interesting problem: write a sed script to substitute every even number with the nearest odd number. I got this one from the list of the latest searches which bring the users to this site.
Let’s see how we can tackle this issue.

First of all, let me say that, for this type of tasks, I’d rather use awk because it has variables and strong and fast arithmetics capabilites. Here is how I’d do in awk:

$ echo "12 15 4 98 1123" | awk '{
for (i=1;i<=NF;i++)
if ($i ~ /^[0-9]*[02468]$/)
$i--
} 1'

The above command ignores numbers mixed with strings (i.e. foo12, 4bar2,etc.) and simply subtracts 1 from any even number (though you can easily add 1 by using $i++ in the place of $i–).

How to know which process is going to be the “next”

If the memory runs out the kernel just use an agent which will try to free some memory by killing those processes which are both old and fat (i.e. use a lot of memory for too long). Every process comes with a score which is constantly updated by the kernel. You can view the score by lurking in /proc/$PID/oom_score.

This article briefly describes how you can find out which process has the biggest change to being killed in a “out of memory” event and how to protect those which are required (e.g. ssh daemon for a remote machine) .

When the OOM killer is called, the process which will most likely be the first victim, is the one with the top score. Here is a little bash script that will show you the running processes sorted by score in ascending order: