I use the following shell syntax (from my previos questione, answered by - phemient) in order to verify if file is ASCII (text) or other

if LC_ALL=C grep -q '[^[:print:][:space:]]' file; then
   echo "file contains non-ascii characters"
   else
     echo "file contains ascii characters only"
fi

the problem is that I get "file contains non-ascii characters" (from the shell script syntax) even if the test_file is ASCII file why?

I also test the file with file command and this is what I get

file test_file 
Non-ISO extended-ASCII English text" its also ASCII file 

my question: how to change the shell syntax in order to support also "Non-ISO extended-ASCII English text" ?

so I will get the print "file contains ascii characters only" from the shell script

  remark the solution must be for Linux and solaris

THX

link|improve this question

79% accept rate
1  
This is your second post on this topic ( first here ). Maybe you could say a few words about why you're trying to accomplish this. Possibly there is a different approach which will be more reliable. – dmckee Oct 27 '10 at 16:00
the second post is about diffrent isshu , – jennifer Oct 27 '10 at 20:23
Correct me if I'm wrong, but you're still working on the same underlying problem, right? I'm not claiming this is a duplicate, rather I'm suggesting that with more context we might be able to provide better help. – dmckee Oct 27 '10 at 20:38
dear dmckee hi this post is continue the first post , the first post from my point is almost close but during my test I notice about little problem , please take a look on my first post many many remarks and if some one want to help its very difficult to understand what I want , therefore I ask the second post to clear this ishu – jennifer Oct 27 '10 at 21:07
feedback

1 Answer

Hi I would use something like:

#!/bin/bash
if [[ `file -b $1` == "ASCII text" ]] ; then
   echo "file contains ascii characters only"
   else
     echo "file contains non-ascii characters"
fi

Looks like:

$ filetest.sh 1.txt  
file contains ascii characters only
$ filetest.sh PHOTOS/HPIM0532.jpg  
file contains non-ascii characters
link|improve this answer
what about Solaris -b option not defined in Solaris - what the same flag in Solaris? (i work on Linux and Solaris) – jennifer Oct 27 '10 at 12:10
then put: if [[ file $1 | cut -d : -f 2 | sed 's/ //' == "ASCII text" ]] ; then – Warnaud Oct 27 '10 at 12:53
@Warnaud hi sorry but I get also: Non-ISO extended-ASCII English text, with very long lines (I used the -b option) my target to define the file as ASCII also if I get the output Non-ISO extended-ASCII English text, with very long lines – jennifer Oct 27 '10 at 13:06
for example: file -b file_test Non-ISO extended-ASCII English text, with very long lines – jennifer Oct 27 '10 at 13:08
please advice according to my first original question how to change the shell syntax in order to support "Non-ISO extended-ASCII English text" – jennifer Oct 27 '10 at 13:10
show 10 more comments
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.