I am trying to use a single command to scan /etc/passwd for the gecos field in the format "User (userid) is (Real name)" so output would be "User brian is Brian". I have been trying for a good hour and cannot come up with a good solution. I'm sure i need to use grep but cannot figure out a good way to do it.
migrated from stackoverflow.com Sep 24 '12 at 17:25
Leave out the |
|||
|
|
|
awk -F \: '{print "User ", $1, "is ", $5;}' /etc/passwd Using awk seems to be the better choice. |
|||
|
|
|
Per Pascal's comment you'll need to use a utility like awk (or Perl, Python, Ruby, etc). This awk command will work for passwd files with comma-separated GECOS fields with the user's real name first:
|
|||
|
|
|
Your question is a bit vague, but all the answers thus far solve the problem of parsing the passwd line into your format. However, your suggestion for using
Since there are so many
Extending the script to take an arbitrary number of arguments is left an exercise for the reader. |
||||
|
|
awk(or Perl, Python, ...)? – Pascal Cuoq Dec 1 '10 at 5:42