If there is a possibility that some records are bad, you need to read the whole thing. If you are just identifying the format, you can read a few lines.
whole file scan
awk -F'|' 'BEGIN{ max=0}
{ if (NF > max ) {max =NF}
END{ print "max fileds found=", max}' inputfile
partial:
head inputfile | awk -F'|' 'BEGIN{ max=0}
{ if (NF > max ) {max =NF}
END{ print "max fileds found=", max}'
Another quick one:
What is the right awk syntax and how to write/redirect records into two files(good and bad):
if (NF!='$temp')
then write to the bad_file.txt
else write to the good_file.txt
fi
The ternary operator is your friend in these simple cases. I started to use the ternary operator but then thought the OP might have more print lines or other operations necessary on either side of the condition, so gave way to the traditional if-then-else.
Understood. The redirection plumbing in awk is setup once, and subsequent > or >> redirections simply continue writing on the already open FD. In shell scripts, the FD is closed at the end of each command.