Saturday, February 4, 2017

Find a line with Control Character

I came into a situation where one of the application was failing to read a file.
There was no clue for that reason and it was just throwing exception of some bad string.
We knew there is something wrong with the file it is trying to read.

So we suspected some control character within the file. In order to find that character we can use the following commands :

Filename: test.txt
Line 1
line 2
line3
linne 4
line 5
line 6

GNU grep :
#/usr/gnu/bin/grep '[^[:print:]]' test.txt
line 5

#/usr/gnu/bin/grep '[[:cntrl:]]' test.txt
line 5

Above example indicates that there is a control character in the above line.

Using "cat" : reveals the control character at line 5 :
#cat -vte test.txt
Line 1$
line 2$
line3$
linne 4$
line 5^M$
line 6$

You can use vi/vim too but if the file is huge it may be difficult.
You can open file in vi and do ":set list" to see control characters