Solutions Log by Dan Reiland

28Feb/110

Updating rubygems on Ubuntu

If you have ever tried to update rubygems on a base Ubuntu 10.04 or 10.10 install you will have been greeted with:

# gem update --system
ERROR:  While executing gem ... (RuntimeError)
    gem update --system is disabled on Debian. RubyGems can be updated using the official Debian repositories by aptitude or apt-get.

You can work around this limitation:

# gem install rubygems-update
# /var/lib/gems/1.8/bin/update_rubygems

You will now be able to update rubygems using the standard set of commands.

Reference:
http://www.tinymission.com/blog/blogengine.web/post/2010/11/03/Updating-to-Rails-30-on-Ubuntu-1004.aspx

Tagged as: No Comments
11Feb/100

Reformat and reindent an XML file

This is easy to accomplish with xmllint and a shell one-liner:

xmllint --format inputfile.xml > outputfile.xml
Tagged as: No Comments
4Dec/090

Replace Pipes with Tabs in a Delimited File

Issue:
It is often necessary to replace delimiters in a file with a form the receiving party expects. sed is my favorite method of meeting the need.

Solution:
Replace double pipes with tabs

sed 's/||/           /g' file.in > file.out

Note: If you find the tab key simply does not work, try CTRL+V+I from your terminal.
Reference: http://forums.devshed.com/unix-help-35/replacing-tabs-with-spaces-372623.html

Caveats:
Special consideration (and a regex) will be required for data where delimiters are present in the data itself. Consider your use case and apply appropriately.

7Oct/090

Create a pidof command to find PID numbers easily

Most UNIX environments include the pidof command which is put to use whenever one needs to quickly determine, by name, the pid of a running program. Apple's Mac OS X lacks the pidof command, however, one may approximate its function with the following shell script:

#!/bin/sh
ps axc|awk "{if (\$5==\"$1\") print \$1}";

Save the script as /bin/pidof and be sure to set its executable bit:

chmod a+x /bin/pidof
Tagged as: , No Comments
6Oct/090

Strip empty (null) lines from a file

sed meets the need; the recipe follows:

sed '/^$/d' filename
25Aug/090

Mirror a Directory Structure Using the Command Line

Issue:
You need to mirror the directory structure (but not contents) for a tree.

Resolution:
Execute the following command from the root of your source directory. Adjust the destination variable to suit your tastes: %i will match the first token, %j the second, %k the third, and %l will match everything else.

for /F "tokens=1,2,3* delims=\" %i in ('dir /Ad /B /N /S') do mkdir t:\dest\%l

Reference: http://www.dostips.com/DtTipsStringManipulation.php

9Jul/092

rsync failed to set times on [filename]

This error occurs because the version of rsync on the system cannot preserve modified times for directories.

Run rsync with the following arguments to suppress this warning:

 rsync -avPO ./source/* ./destination

Explanation of switches:

1
2
3
4
a -> Archive mode (do not preserve hard links, ACLs, or extended attributes)
v -> Verbose (I like to know what is happening)
P -> Equivalent to --partial --progress (for long transfers that may be interrupted)
O -> Omit directories from times
Tagged as: , 2 Comments
8Jul/090

Create thumbnails en-masse from a bash prompt

A simple one-liner and ffmpeg, basename, and cut gets this done.

1
for i in *.f4v; do ffmpeg -i `basename $i` -s 320x240 `basename $i | cut -d'.' -f1`.jpg; done

Thumbnail output size is configurable with the -s switch.

25Jun/090

Count the number of characters (or columns) in a file

Sometimes you want to count the number of characters or columns in a file.

1
awk 'length>max{max=length}END{print max}' filename.txt

For extraordinarily long or wide data sets you may want to consider the following:

1
head -n 10 | awk 'length>max{max=length}END{print max}'

OR

1
tail -n 10 | awk 'length>max{max=length}END{print max}'
24Jun/090

Convert text file to UTF-8 encoding from the shell

1
for f in *.txt; do iconv -f mac -t utf-8 "$f" >"$f.utf8"; done

Note:
-f Input file encoding type
-t Output file encoding type

Reference: http://manual.macromates.com/en/saving_files.html