How to check text file have control character or not by shell script

How to check text file have control character or not by shell script

am 07.04.2008 06:31:02 von moonhkt

Hi All
How to check text file have control character or not by shell script ?

moonhk

Re: How to check text file have control character or not by shellscript

am 07.04.2008 06:39:49 von Ed Morton

On 4/6/2008 11:31 PM, moonhkt wrote:
> Hi All
> How to check text file have control character or not by shell script ?
>
> moonhk

grep -q '[[:cntrl:]]' file && echo "yes" || echo "no"

Re: How to check text file have control character or not by shell script

am 07.04.2008 06:49:20 von James Michael Fultz

* moonhkt :
> Hi All
> How to check text file have control character or not by shell script ?
>
> moonhk

# replace with the escape sequence of the desired
# character, e.g. \t or \011 for TAB
before=$(wc -c < file)
after=$(tr -d '' < file | wc -c)

if [ "$before" -gt "$after" ]
then
echo "Control character found"
else
echo "Control character not found"
fi

--
James Michael Fultz
Remove this part when replying ^^^^^^^^

Re: How to check text file have control character or not by shell script

am 07.04.2008 12:05:52 von PK

Ed Morton wrote:

>
>
> On 4/6/2008 11:31 PM, moonhkt wrote:
>> Hi All
>> How to check text file have control character or not by shell script ?
>>
>> moonhk
>
> grep -q '[[:cntrl:]]' file && echo "yes" || echo "no"

For very large files, the nonstandard -m option to grep (eg, -m 1) may
provide a slight performance improvement.
If that is not possible, then with awk:

awk '/[[:cntrl:]]/ {print "yes";exit}' file

(although the output is a bit different)

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: How to check text file have control character or not by shell

am 07.04.2008 13:55:49 von Janis Papanagnou

On 7 Apr., 12:05, pk wrote:
> Ed Morton wrote:
>
> > On 4/6/2008 11:31 PM, moonhkt wrote:
> >> Hi All
> >> How to check text file have control character or not by shell script ?
>
> >> moonhk
>
> > grep -q '[[:cntrl:]]' file && echo "yes" || echo "no"
>
> For very large files, the nonstandard -m option to grep (eg, -m 1) may
> provide a slight performance improvement.

I'd assume that the standard option -q exits immediately if something
is
found, so I wonder why the non-standard -m option would be more
performant.
The standard seems not quite exact on that; but the GNU man page at
least
contains the word "immediately".

-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immedi-
ately with zero status if any match is found, even if an error
was detected. Also see the -s or --no-messages option.


Janis

> If that is not possible, then with awk:
>
> awk '/[[:cntrl:]]/ {print "yes";exit}' file
>
> (although the output is a bit different)
>
> --
> All the commands are tested with bash and GNU tools, so they may use
> nonstandard features. I try to mention when something is nonstandard (if
> I'm aware of that), but I may miss something. Corrections are welcome.

Re: How to check text file have control character or not by shell script

am 07.04.2008 14:28:54 von PK

Janis wrote:

> I'd assume that the standard option -q exits immediately if something
> is found, so I wonder why the non-standard -m option would be more
> performant.

If that is the case, of course there would be no difference (see below).

> The standard seems not quite exact on that; but the GNU man page at
> least contains the word "immediately".
>
> -q, --quiet, --silent
> Quiet; do not write anything to standard output. Exit immedi-
> ately with zero status if any match is found, even if an error
> was detected. Also see the -s or --no-messages option.

Ah thanks. I didn't read the GNU page this time, so I found nothing in the
standard indicating that behavior for the the -q option. If that is the
case, well, then -q is definitely better.

Thanks

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: How to check text file have control character or not by shell script

am 07.04.2008 15:06:34 von PK

pk wrote:

> Ah thanks. I didn't read the GNU page this time, so I found nothing in the
> standard indicating that behavior for the the -q option. If that is the
> case, well, then -q is definitely better.

Ok, it's specified later, near the end, in the "application usage"
paragraph:

"The -q option provides a means of easily determining whether or not a
pattern (or string) exists in a group of files. When searching several
files, it provides a performance improvement (because it can quit as soon
as it finds the first match) and requires less care by the user in choosing
the set of files to supply as arguments (because it exits zero if it finds
a match even if grep detected an access or read error on earlier file
operands)."

Thanks!

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.