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

awkpasswdtext processingwildcards

I have an example /etc/passwd file like this:

tom:x:1000:1000:Work:/home/tom:/bin/bash
george:x:1000:1000:Work:/home/george:/bin/bash
bla:x:1000:1000:Work:/home/bla:/bin/bash
boo:x:1000:1000:Work:/home/boo:/bin/bash
bee:x:1000:1000:Work:/root/list:/bin/bash

I'm trying to list all users with a home folder in /home/.

I wrote

cat ~/Desktop/e.txt |awk -F ":" '{if ($6 ~/^/home/) print $1;}'

where e.txt is the text I copied here.

I understand there is a problem with the backslash which is an escape character, but how do I fix it so I can list them in one line of a command?

Best Answer

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
Related Question