Friday, May 21, 2010

Methods to Generate a Random digit from 0 to 9 in bash

echo "${RANDOM}" | cut -c 3 | grep .

Note that sometimes you will get nothing...

A more sophisticated way to do this is to use modular arithmatic:

digit="${RANDOM}" && let "digit %= 10" && echo $digit

This will always return a value. Note that the first version is essentially 100-999 mod 100.

Thursday, May 20, 2010

Rough Sed and Awk Code to Convert a CSV file to a sortable Mediawiki table

echo -e "3,4,5,5\n4,5,6,8" | sed "s/$/\n-/" | sed -e "s/,/\n/g" | sed -e "s/^/|/" | awk 'BEGIN {
    print "{|class=\"wikitable sortable\" style=\"vertical-align:top; background:lightblue;\"\n!key value!!col 1!!col 2!!col 3\n|-"
    }
    {
    print
    }
    END {
    print "|}"
    }
  '