How to check text file have control character or not by shell script
am 07.04.2008 06:31:02 von moonhktHi All
How to check text file have control character or not by shell script ?
moonhk
Hi All
How to check text file have control character or not by shell script ?
moonhk
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"
* moonhkt
> Hi All
> How to check text file have control character or not by shell script ?
>
> moonhk
# replace
# character, e.g. \t or \011 for TAB
before=$(wc -c < file)
after=$(tr -d '
if [ "$before" -gt "$after" ]
then
echo "Control character found"
else
echo "Control character not found"
fi
--
James Michael Fultz
Remove this part when replying ^^^^^^^^
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.
On 7 Apr., 12:05, 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.
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.