DISCLAIMER : Please note that blog owner takes no responsibility of any kind for any type of data loss or damage by trying any of the command/method mentioned in this blog. You may use the commands/method/scripts on your own responsibility.If you find something useful, a comment would be appreciated to let other viewers also know that the solution/method work(ed) for you.


UNIX one-liners continued

These one liners may be of help to identify user or group info...

1. To extract only the userid from the /etc/passwd file based on a string sequence in the GECOS field.

    cat /etc/passwd | awk -F":" '/\[I]/ { print $1 }'
                        OR
    cat /etc/passwd | awk -F":" '/GB\/I/ { print $1 }'


2.  To list records in /etc/passwd of users who are members of  the staff group.

    grep <groupname>  /etc/group | awk -F":" '{ print$4}'  | sed 's/\,/ /g'  | xargs -n1 echo  > patFile ; grep -f  patFile  /etc/passwd ; rm patFile;

    grep staff /etc/group | awk -F":" '{ print$4}'  | sed 's/\,/ /g'  | xargs -n1 echo  > patFile ; grep -f  patFile  /etc/passwd ; rm patFile;

3. To list userids and home directories of users of the staff group.

    grep <groupname> /etc/group | awk -F":" '{ print$4}'  | sed 's/\,/ /g'  | xargs -n1 echo  > patFile ; grep -f  patFile  /etc/passwd  | awk -F ":" '{ print $1, $6}'; rm patFile;

    grep staff /etc/group | awk -F":" '{ print$4}'  | sed 's/\,/ /g'  | xargs -n1 echo  > patFile ; grep -f  patFile  /etc/passwd  | awk -F ":" '{ print $1, $6}'; rm patFile;


4. To list all the groups of which a userid is a member of

    grep <userid>  /etc/group | awk -F":" '{ print $1 }'

    grep sabari /etc/group | awk -F":" '{ print $1 }'

5. To list all users of a group and further grep them by a department signifier in the GECOS field.

    grep <groupname> /etc/group | awk -F":" '{ print$4}' | sed 's/\,/ /g'  | xargs -n1 echo  > patFile ;  grep -f  patFile  /etc/passwd | egrep \/I\/ | awk -F":" '{ print $1," ", $5 }'; rm patFile;

    grep sys group.file | awk -F":" '{ print$4}' | sed 's/\,/ /g'  | xargs -n1 echo  > patFile ;  grep -f  patFile  /etc/passwd | egrep \/I\/ | awk -F":" '{ print $1," ", $5 }'; rm patFile;


6. To print only one particular field from a file

        cat <filename> |awk '{ print $5 }'