Printing /etc/passwd in different output

i would like to write a script that prints /etc/passwd as this output:

root daemon bin sys …
x x x x
0 1 1 2
root daemon bin sys

first line has all user accounts in same row
second line has all encrypted passwords (x) in same row
third line has all user ids in same row
and etc…

The following will print the entire file and each field is separated by the :

awk -F':' '{ print $0 }' /etc/passwd

To print encrypted you need to use the /etc/shadow file and command must be run as the root user:

sudo awk -F':' '{ print $0 }' /etc/shadow

Printing /etc/passwd in different output using fields

Now try field wise outputs and store them in a shell variable (put all in your shell script):

eusers="$(awk -F':' '{ print $1 }' /etc/passwd)"
epasswords="$(sudo awk -F':' '{ print $2 }' /etc/shadow)"
euids="$(awk -F':' '{ print $3 }' /etc/passwd)"

# Remove \n chracter from echo input
eusers="${eusers//$'\n'/ }"
epasswords="${epasswords//$'\n'/ }"
epasswords="${epasswords//$'\n'/ }"
euids="${euids//$'\n'/ }"

# print it
echo "${eusers}"
echo "$epasswords"
echo "$euids"

See the following references:

  1. Understanding /etc/shadow file format on Linux - nixCraft
  2. Understanding /etc/shadow file format on Linux - nixCraft
  3. How To Use Bash Parameter Substitution Like A Pro - nixCraft