Things I learned

Line Buffering

So I had this problem where I 'tail'-ed a file, and 'awk'-ed the results, and that worked fine.

After that I wanted to further 'sed' the outcome of the 'awk', and there's where things went wrong, i.e. no output.

So after a decent search I found that 'awk' buffers the output, and I had to 'unbuffer' that. Some 'awk's seem to have an option to switch that on or off, but not on FreeBSD (at least, I didn't find it). So I found something cool:

stdbuf -oL

Put this in front of the buffering command unbuffers the output, and then things start working again:

Exception handling in PHP

So today I ripped my hair (the few that's left) from my head, because my code didn't work as supposed, but no error was shown by my try/catch block, when accessing my database.

So this was the code I worked with:

$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
$dbo = new PDO($dsn, DB_USER, DB_PASS);

and I had to add this, to actually trigger some exceptions:


Extracting a date from a file name

Whenever you have a shell script that handles a file with a date (or some other information) and you want to use that, you can do the following:


# obviously, my file is a csv-file, and I'm stripping the extension and the path.
# the file is called somefile-20160308.csv
FILE=`basename /somepath/somefile* .csv`

# the following line extracts the date
DATE=`expr "$FILE" : ".........\(........\)"`

echo $FILE
echo $DATE

Select an a tag with some particular href

Sometimes, you want to not select all a-tags in HTML, but only particular, for instance based on (part of) the url they're pointing to.

That can be done like this:


These are the attributes

= is exactly equal
!= is not equal
^= is starts with
$= is ends with
*= is contains
~= is contains word
|= is starts with prefix (i.e., |= "prefix" matches "prefix-...")

Running mean in MySQL

So I wanted a graph, which showed the actual temperature for the last 48 hours, and a running mean to go with it:

SELECT convert_tz(date,'gmt','cet') AS date, temperature, 
(CASE WHEN (@n := @n + 1) IS NULL THEN NULL 
    WHEN (@cumval := @cumval + temperature) IS NULL THEN NULL
    ELSE (@cumval / @n) END) AS runningmean 
FROM weather 
CROSS JOIN (SELECT @n := 0, @cumval := 0) AS vars 
WHERE convert_tz(date,'gmt','cet') >= now() - interval 48 hour 

Speaks for itself

ytics in gnuplot

In GnuPlot you can add extra tics to your y-axis (or x-axis, whatever you like) by adding this

set ytics add ("label" value)

for instance

set ytics add ("max" STATS_max_y)
set ytics add ("min" STATS_min_y)

Variables in gnuplot

Gnuplot gives you lots of extra values, if you use the following commands:

data = "datafile.dat"
# version > 4.2
stats data 1:2
# first a dummy plot
plot data u 1:2

And the following command reveals them

show variables