Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate record format

Status
Not open for further replies.

gxh1000

Programmer
Aug 4, 1999
10
US
I need to validate that each record contains exactly 5 comma separated fields in my file.

ex. a,b,c,d,e

If any record doesn't match the format, then fail. Thanks for your help.
 
nawk -f val.awk data.txt
if [ $? -ne 0 ] ; then
echo "failed"
fi

#--------------- val.awk
BEGIN {
FS=","
st="0"
}

NF != 5 { st=1; exit }

END {
exit st;
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Try something like this:
Code:
awk -F, 'NF!=5' /path/to/yourfile >/tmp/check$$
[ -s /tmp/check$$ ] && {
  echo &quot;PROBLEM with fields:&quot;; cat /tmp/check$$
  exit 1
}

Hope This Help
PH.
 
This should get you started...
Code:
BEGIN {
  FS=&quot;,&quot;
}
NF != 5 {
  # fail code goes here
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top