11
Feb/10
0

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:
4
Dec/09
0

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.

7
Oct/09
0

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: ,
6
Oct/09
0

Strip empty (null) lines from a file

sed meets the need; the recipe follows:

sed '/^$/d' filename
25
Aug/09
0

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

9
Jul/09
1

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: ,
8
Jul/09
0

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.

25
Jun/09
0

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}'
24
Jun/09
0

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

15
Jun/09
0

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