How to get the Invalid records from a file using awk?

My Input file is fixed length record ends with . as end of the line and the character length is 4156
Example:

12234XYZ     TY^4253$+00000-00000...........

I need to check is there any control characters(like ^M,^Z)
The line will be splitted

awk
'{id=substr($0,1,5)
nm=substr($0,6,3)
ad=substr($0,10,5)
ct=substr($0,15,8)

if ( id ~ /^ *$/ )
{
       a1="Invalid Id:EMTPY"
}
if ( id !~ /^[0-z]*$/ )
{
    a2="Invalid Id:"id
}
if ( nm ~ /^ *$/ )
{
     a3="Invalid Name:Empty"
}
if ( nm !~ /^[a-z]*$/)
{   
    a4="Invalid Name:" nm
}
like so on
.
....
if (  id !~ /^[0-z]*$/ || nm !~ /^[a-z]*$/ || nm !~ /^ *$/ || ......)
        {
             print id":record valid"
        }
else
      {
      print id-a1,a2,a3,a4...
 }
}'

the outptut should be

id-Invalid Name:Empty Invalid ad:@%!^%@! Invalid ct:8889%% 

Is there any way to reduce the if conditions.. or the best way to achieve the ouput

I would use something more like:

awk '
{	err = ""
	id=substr($0,1,5)
	nm=substr($0,6,3)
	ad=substr($0,10,5)
	ct=substr($0,15,8)
	# ...

	if(id ~ /^ *$/)
		err = " Invalid Id:EMTPY"
	else if(id !~ /^[0-z]*$/)
		err = " Invalid Id:" id
	if (nm ~ /^ *$/)
		err = err " Invalid Name:Empty"
	if ( nm !~ /^[a-z]*$/)
		err = err " Invalid Name:" nm
	# like so on .  ....
	if(err == "")
		print id":record valid"
	else	print substr(err, 2)
}' file

Note that the opening single-quote for the awk program operand MUST be on the same line as awk .

Note that the 1st field's mutually exclusive failed tests are of the form err = " message" and all subsequent failed tests are of the form err = err "message" .

Note that the leading <space> character in each of the error messages is crucial.

Note that if you're trying to test whether or not id is an alphanumeric string, you're using the wrong ERE. You should use something more like:

if(id !~ /^[[:alnum:]]*$/)

which will add an error message to err if there are any non-alphanumeric characters in id . Your current test will also allow colon, semicolon, less-than, equal-sign, greater-than, question-mark, and at-sign characters in id if you're using a codeset that is a superset of US-ASCII and is an ERE producing undefined results if you're using an EBCDIC codeset.

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

You could use the character class [[:cntrl:]] for the validity check.