100 Useful Command-Line Utilities

by Oliver; 2014

61. od

One of the most ninja moves in unix is the od command which, with the -tc flag, explicitly prints every character in a string or a file. For example:
$ echo joe | od -tc
0000000   j   o   e  \n
0000004
We see everything: the j, the o, the e, and the newline. This is incredibly useful for debugging, especially because some programs—notably those that bear the Microsoft stamp—will silently insert evil "landmine" characters into your files. The most common issue is that Windows/Microsoft sometimes uses a carriage-return (\r), whereas Mac/unix uses a much more sensible newline (\n). If you're transferring files from a Windows machine to unix or Mac and let your guard down, this can cause unexpected bugs. Consider a Microsoft Excel file:

image

If we save this spreadsheet as a text file and try to cat it, it screws up? Why? Our od command reveals the answer:
$ cat Workbook1.txt | od -tc
0000000   1  \t   2  \r   1  \t   2  \r   1  \t   2
0000013
Horrible carriage-returns! Let's fix it and check that it worked:
$ cat ~/Desktop/Workbook1.txt | tr "\r" "\n" | od -tc
0000000   1  \t   2  \n   1  \t   2  \n   1  \t   2
0000013
Score!

<PREV   NEXT>