Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for an easy, portable way to validate the number of fields in /etc/passwd, /etc/shadow, /etc/group and /etc/master.passwd. This would run on FreeBSD, Linux and other Un*xes. pwck does this, and more, but it only runs on Linux-based systems.

How can I count the number of fields separated by colons in /etc/passwd?


Following the example in "grep: count total number of occurrences", I came up with the following quick hack:

cat /etc/passwd | while read LINE; do echo $LINE | grep -o ':'  |wc -l; done

But I'm looking for a better way.

share|improve this question
up vote 7 down vote accepted

awk -F: ' NF!=7 {print}' /etc/passwd

should do it. If the number of fields is not 7, then print it out.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.