I have to take user input of there UID then use it to reference the /etc/passwd directory and sort the data by first name and display only username,firstname,lastname in that order. The problem i'm having is sorting the output. if i use a command like

cat /etc/passwd |grep  $id_prefix | sort | cut -d: -f1,5

The output sorts by username and displays in a lastname, firstname order. I know I could change the command to something like.

cat /etc/passwd |grep  $id_prefix | sort | cut -d: -f5 | sed 's/\([^ ]*\)[ ][ ]*\([^ ]*\)/\2 \1/'

Then I will get the output sorted and cut how I want it but it will not contain FIELD1, the usernames. How would I write this to correctly display all the fields I need and also sort them?

  • 3
    It would be easier if you could show some sample of input, and desire output. – jimmij Oct 24 '14 at 0:05
  • The input would be a user id prefix, for instance all users in my class have the same first 5 letters for a user id and the rest is unique. so my input would be for example ClassA and this is where I am using grep because it will ignore any users from other classes. ClassB, ClassC ect. The output should show ( UserID , FirstName, LastName,) – eric botelho Oct 26 '14 at 15:30
  • please create a minimal sample of your input file and add it to your question. It is hard to understand what you are trying to do from above description. – jimmij Oct 26 '14 at 19:45

You could do something like this:

awk -F: '{ print $1"   "$5 }' /etc/passwd | sort -k2

This will parse the username and comment fields from /etc/passwd and sort on the second field which will be the first name of the user, provided they are listed in first name and last name order.

Here is an example of incorporating grep:

grep "500\|501\|502" /etc/passwd | awk -F: '{ print $1"    "$5 }' | sort -k2

jira    Atlassian JIRA
dick    Richard Jameson
bob    Robert Gillespie


Use this to reverse the lastname, firstname ordering from the /etc/passwd file:

awk -F: '{ print $1"   "$5 }' /etc/passwd | awk '{ print $1"    "$3" "$2 }' | sort -k2

The above example modified for lastname, firstname in the /etc/passwd file:

grep "500\|501\|502" /etc/passwd | awk -F: '{ print $1"    "$5 }' |  awk '{ print $1"    "$3" "$2 }' | sort -k2

This works if there are not, and should not be, any commas separating first/last, last/first names.

EDIT (the second):

Since the last/first names are comma separated, this should work:

awk -F: /$id_prefix/'{ print $1"    "$5 }' /etc/passwd | awk -F, '{ print $1"    "$2 }' | awk '{ print $1"    "$3"    "$2 }' | sort -k2

Thank you @Jidder for recommending not piping from grep to awk.

| improve this answer | |
  • They are sorted by LastName, FirstName, in the /etc/passwd file which is what I am having trouble with because essential I have to reverse the middle field and last field of my output – eric botelho Oct 26 '14 at 15:32
  • The /etc/passwd file actually does use a comma as a delimiter between lastName FirstName. I am not that familiar with awk in general but if I used a , instead of the space between $1 and $5 would that work? – eric botelho Oct 28 '14 at 0:51

You have to do it:

grep mohsen /etc/passwd | awk -F':' {'split($5,a,",");printf "username is :%s ,uid is: %s , name is %s \n",$1,$3,a[1]  '}

Output is:

username is :mohsen ,uid is: 1000 , name is Mohsen Pahlevanzadeh
| improve this answer | |
  • This keeps returning an unterminated string error I couldn't find where it was though. – eric botelho Oct 26 '14 at 16:14
  • please replace your username with mohsen.I got answer. – PersianGulf Oct 26 '14 at 22:33

I think you could get away with something like this:

grep $id_prefix /etc/passwd | cut --delimiter: --fields=1,5 | sort --field-separator=: --key=2

You could also add --ignore-case option to sort if needed in your context. Check its docs for more options.

| improve this answer | |

Don't know why people are piping grep into awk.
You could do it all in awk. This presumes that first and last name are separated by a space.
Where USERNAME is the search term

awk -F: '/USERNAME/{split($5,a," ");print $1,a[2],a[1]}' etc/passwd
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.