Solutions Log by Dan Reiland

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
9Jul/091

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 -avrPO ./source/* ./destination

Explanation of switches:

1
2
3
4
5
a -> Archive mode (do not preserve hard links, ACLs, or extended attributes)
v -> Verbose (I like to know what is happening)
r -> Copy directories recursively
P -> Equivalent to --partial --progress (for long transfers that may be interrupted)
O -> Omit directories from times
Tagged as: , 1 Comment
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

15Jun/090

Mac OSX does not contain seq

seq is used for generating a series of numbers.

Mac OSX does not contain this. To overcome the limitation, install MacPorts and issue the following command:

1
sudo port install seq

You will also note that seq is not available from a shell prompt, that is because it exists as gseq. Symbolically link it:

1
sudo ln -s /opt/local/bin/gseq /opt/local/bin/seq
Tagged as: No Comments
11Jun/090

Sort files according to their size

1
du -s * | sort -n