1

I am having trouble using awk to count the number of /bin/false entries in a file in field7 with a single command.

I can use grep no problem. I can use awk to count a few different ways. I have this:

awk '/false/{print NR}' /etc/password  

but it's not printing "just the count of lines" that have bin/false in the 7th field. I need to incorporate $7 somehow but can't find any help with it. Can someone please point me in the right direction.

daemon:x:2:2:Daemon:/sbin:/bin/bash
ftp:x:40:49:FTP account:/srv/ftp:/bin/bash
daemonuser:x:50:59:nouser/bin/false:/home/nouser:/bin/bash
gdm:x:106:111:Gnome Display Mgr daemon:/var/lib/gdm:/bin/false
haldaemon:x:101:102:User for haldaemon:/var/run/hald:/bin/false
lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash
mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false
root:x:0:0:root:/root:/bin/bash
sshd:x:71:65:SSH daemon:/var/lib/sshd:/bin/false
olivert:x:1001:1003:Tom Oliver:/home/olivert:/bin/csh
smiths:x:1049:1000:Sue Williams:/export/home/smiths:/bin/csh
northj:x:1003:1003:Jim jones-North:/home/northj:/bin/csh
denniss:x:1005:1003:Sue Dennis:/home/denniss:/bin/bash
smitha:x:1050:1001:Amy Smith:/export/home/smitha:/bin/bash
jonesc:x:1053:1001:Cathy Jones:/export/home/jonesc:/bin/ksh
smithd:x:1055:1001:Dan Smith Jr:/export/home/smithd:/bin/csh
3
awk -F: '$7 ~ /\/bin\/false/ { print }' /etc/passwd | wc -l
  • -F: tells awk to use : as the field separator
  • $7 ~ /\/bin\/false/ compares only field 7.
  • pipe the output into wc -l to count the lines.

Alternatively, without using wc -l:

awk -F: '$7 ~ /\/bin\/false/ { count++ } END {print count}' /etc/passwd

You might find this of interest too:

$ awk -F: '{ print $7 }' /etc/passwd | sort | uniq -c
     38 /bin/bash
     79 /bin/false
     11 /bin/sh
      1 /bin/sync
      1 /bin/true
      1 /usr/local/bin/ftponly
     17 /usr/sbin/nologin
| improve this answer | |
  • Thank you for your help. I am using putty. When entered it gives an error awk: warning escape sequence '\/' treated as plain '/' I tried using the alternative – jake Nov 13 '15 at 6:12
  • what version of awk are you using? – cas Nov 13 '15 at 6:14
  • gnu awk version 4.0.1 – jake Nov 13 '15 at 6:17
  • Im sorry beginner mistake it works. – jake Nov 13 '15 at 6:20
  • 1
    I would have used $7 == "/bin/false" – Archemar Nov 13 '15 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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