Lesser known linux commands : od

od dumps file in octal and other formats. od can be used to
view file contents byte-by-byte in different formats – character,
octal or hexadecimal numbers.

e.g
$ od -c hello.c

0000000 # i n c l u d e < s t d i o . h
0000020 > \n # t h i s i s f i r s t
0000040 \t p r o g r a m e \n i n t m
0000060 a i n ( ) { \n \t p r i n t f ( ”
0000100 H e l l o w o r l d \ n ” ; \n }
0000120 \n
0000121

Here I am using -c option to display in character format.
Now compare the above with simple cat output

$ cat hello.c
#include<stdio.h>
#this is first programe
int main(){
printf(“Helloworld\n”;
}

Under cat output you can see spaces between ‘first’ and ‘programme’. Acutually it is a space and tab character and not just spaces. This is clearly visible in od output – 0000040 line.

Where is this useful? One situation where I use frequently is loading data from text files into database tables. Usually the text file data will contain one line per row and each column separted by a tab. Somehow, a tab may be present in the column data itself, thus upsetting the structure of the row (it has more tabs than the columns). Loading such data using any bulk insert options of most databases will fail.

An examination of the failing row using od can help us identify the extra tab. Using editor the tab can be removed and problem fixed.

od has several option like -x hexadecimal, -o octal etc.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: