Process /etc/passwd file to list all users whose home folder is in /home

You can use quotes instead of / for the regex, to avoid escaping the /:

awk -F: '$6 ~ "^/home/" {print $1}' ~/Desktop/e.txt

With awk, {if (foo) ... } can often be simplified to just foo {...}.


You may escape forward slashes as shown below:

awk -F':' '$6~/^\/home\//{ print $1 }' ~/Desktop/e.txt

Another trick would be using complex field separator:

awk -F'[:/]' '$7=="home"{ print $1 }' ~/Desktop/e.txt
  • -F'[:/]' - treat both : and / to be a field separator

Assuming e.txt is actually /etc/passwd, you should actually use getent passwd instead of parsing the file since user account information may be stored in multiple locations specified in /etc/nsswitch.conf such as LDAP.

getent passwd | awk -F ':' '$6 ~ "^/home"'

Note that the print statement is implied when the condition is true. That will print the whole line, though. This will print only the user name:

getent passwd | awk -F ':' '$6 ~ "^/home" {print $1}'