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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the Invalid records from a file using awk?
# 1  
Old 04-30-2016
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:
Code:
12234XYZ     TY^4253$+00000-00000...........


I need to check is there any control characters(like ^M,^Z)
The line will be splitted
Code:
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
Code:
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

Last edited by Scrutinizer; 04-30-2016 at 01:56 AM.. Reason: Corrected and moved code tags
# 2  
Old 04-30-2016
I would use something more like:
Code:
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:
Code:
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.
# 3  
Old 04-30-2016
You could use the character class [[:cntrl:]] for the validity check.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk/sed/cut to filter out records from a file based on criteria

I have two files and would need to filter out records based on certain criteria, these column are of variable lengths, but the lengths are uniform throughout all the records of the file. I have shown a sample of three records below. Line 1-9 is the item number "0227546_1" in the case of the first... (15 Replies)
Discussion started by: MIA651
15 Replies

2. Shell Programming and Scripting

awk - compare records of 1 file with 3 files

hi.. I want to compare records present in 1 file with those in 3 other files and print those records of file 1 which are not present in any of the files. for eg - file1 file2 file3 file4 1 1 5 7 2 2 6 9 3 4 5 6 7 8 9 ... (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

3. Shell Programming and Scripting

awk script for getting the selected records from a file.

Hello, I have attached one file named file.txt . I have to create a file using the awk script with the records in which 38th position is P and not V . ex it should have 00501 HOLTSVILLE NYP00501 and it should not include 00501 I R S SERVICE CENTER ... (3 Replies)
Discussion started by: sonam273
3 Replies

4. Shell Programming and Scripting

Capturing the invalid records to error file

HI, I have a source file which has the below data. Tableid,table.txt sourceid,1,2,3,4,5,6 targetid,1,2,3,4,5,6 Tableid,table sourceid,1,2,3,4,5,6 targetid,1,2,3,4,5,6 Tableid,table.txt sourceid,1,2,3,4,5,6 targetid,1,2,3,4,5,6 Tableid,table sourceid,1,2,3,4,5,6 targetid,1,2,3,4,5,6... (6 Replies)
Discussion started by: shruthidwh
6 Replies

5. Shell Programming and Scripting

awk - splitting 1 large file into multiple based on same key records

Hello gurus, I am new to "awk" and trying to break a large file having 4 million records into several output files each having half million but at the same time I want to keep the similar key records in the same output file, not to exist accross the files. e.g. my data is like: Row_Num,... (6 Replies)
Discussion started by: kam66
6 Replies

6. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

7. Shell Programming and Scripting

Filter records in a file using AWK

I want to filter records in one of my file using AWK command (or anyother command). I am using the below code awk -F@ '$1=="0003"&&"$2==20100402" print {$0}' $INPUT > $OUTPUT I want to pass the 0003 and 20100402 values through a variable. How can I do this? Any help is much... (1 Reply)
Discussion started by: gpaulose
1 Replies

8. UNIX for Dummies Questions & Answers

AWK ??-print for fields within records in a file

Hello all, Would appreciate if someone can help me out on the following requirement. INPUT FILE: -------------------------- TPS REPORT abc def ghi jkl mon pqr stu vrs lll END OF TPS REPORT TPS REPORT field1 field2 field3 field4 field5 field6 (8 Replies)
Discussion started by: hyennah
8 Replies

9. Shell Programming and Scripting

awk script required for finding records in 1 file with corresponding another file.

Hi, I have a .txt file (uniqfields.txt) with 3 fields separated by " | " (pipe symbol). This file contains unique values with respect to all these 3 fields taken together. There are about 40,000 SORTED records (rows) in this file. Sample records are given below. 1TVAO|OVEPT|VO... (2 Replies)
Discussion started by: RRVARMA
2 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question