Awk Trick: Verify the /etc/passwd Lines

As you know, the /etc/passwd file has 7 fields on each line. The lines from /etc/passwd having more than 7 fields can create security vulnerabilities or generate massive system errors. It is better to remove them

This little awk oneliner prints the lines having more or less fields than 7 (the broken lines):

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

The fields are delimited by colons (:).

Testing if the awk trick works:

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

This test line is broken because it has 8 fields.

Create a backup for the /etc/passwd file (in case you damage the /etc/passwd file) and after that, remove the broken line by hand.

Easy way to backup the /etc/passwd file:
$ sudo cp /etc/passwd{,.bak}
$ ls /etc | grep passwd
passwd
passwd.bak

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!