Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find non-printable characters in the file?

I tried to find out the unprintable characters in data filein unix. Code :

#!/bin/ksh
export SRCFILE='/data/temp1.dat'
while read line 
do
len=lenght($line)
for( $i = 0; $i < $len; $i++ ) {

        if( ord(substr($line, $i, 1)) > 127 )
        {
            print "$line\n";
            last;
        }
done < $SRCFILE

The code is not working , please help me in getting a solution for the above query.

like image 257
user3759763 Avatar asked Sep 08 '14 08:09

user3759763


2 Answers

You can use grep for finding non-printable characters in a file, something like the following, which finds all non-printable-ASCII and all non-ASCII:

grep -P -n "[\x00-\x1F\x7F-\xFF]" input_file

-P gives you the more powerful Perl regular expressions (PCREs) and -n shows line numbers.

If your grep doesn't support PCREs, I'd just use Perl for this directly:

perl -ne '$x++;if($_=~/[\x00-\x1F\x7F-\xFF]/){print"$x:$_"}' input_file
like image 138
paxdiablo Avatar answered Sep 20 '22 21:09

paxdiablo


You may try something like this :

grep '[^[:print:]]' filePath
like image 23
blackSmith Avatar answered Sep 19 '22 21:09

blackSmith