A couple of useful scripts

I’m posting this mostly for my own reference, but maybe it will be of use to others too.

I’m teaching this quarter in the Information School at the UW (INFO447, Computer Supported Cooperative Work, syllabus here). Three times during the quarter the students are giving presentations using the Ignite! style of presentations (five minutes, 20 slides, slides auto-advance every 15 seconds). I needed to be able to randomly mix up the order of the students in the class so they wouldn’t come in the same order each time.

I started with a class list file that has the names of the students, one per line. Here’s the shell script I used to output a randomly ordered (or unordered) list of those names. It outputs a file named the same as the input file, with “.new” appended to the file name. I run it from the terminal on my Mac. It gives an error message (“line 12: syntax error in expression”) but the output file is there just fine. I have this saved as shuffle.script.


if [[ $# -eq 0 ]]
echo "Usage: $0 [file ...]"
exit 1

for i in "$@"
perl -MList::Util -e 'print List::Util::shuffle ' $i > $i.new
if [[ `wc -c $i` -eq `wc -c $i.new` ]]
mv $i.new $i
echo "Error for file $i!"

The class list file I have has the names in lastname, firstname order. I wanted to show the names in the more friendly firstname lastname order. Here’s the one line awk script that reorders them based on where the comma occurs:

awk -F"," '{print $2,$1}'

I save this as reversenames.script. To execute it from the command line, use reversenames.script < yourinputfile


One thought on “A couple of useful scripts”

  1. And here’s another one – this one outputs the first and last fields of every line (fields delimited by a space character). Useful for stripping middle names from the class list file.

    awk -F" " '{print $1, $NF}'


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s