Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
dscl . -readall /Users 

outputs something like this (only a part of the entire output):

PrimaryGroupID: 20
RealName:
Johnny Smith
RecordName: johnny
RecordType: dsRecTypeStandard:Users
UniqueID: 501
UserShell: /bin/bash

I have this command:

dscl . -readall /Users UniqueID | awk '/^RecordName:/ {name=$2}; /^UniqueID: / {if ($2 > 500) print name}'

It basically outputs all the RecordName's with a UniqueID higher than 500.

I also want to print the RealName, UserShell, RecordType, PrimaryGroupID, and possibly any other category if the UniqueID is greater than 500, but I'm having some difficulty with the syntax of awk.

How would I go about doing this?

Some sample output:

<primarygroupid>20</primarygroupid> 
<realname>Johnny Smith</realname>
...
<uniqueid>501</uniqueid> 

A format that I can access quickly for reading and placing into a variable for a program.

share|improve this question
    
Do you necessarily have to use awk? OS X offers so many more tools you could exploit. –  slhck Oct 12 '12 at 14:09
    
I'm using this in an objective-c script using sh -c. if you have any other suggestions, I'm open to trying them. –  John Oct 12 '12 at 18:06

1 Answer 1

Unfortunately I have absolutely no experience with OSX or the dscl utility. However I do have some awk experience. I've also had a quick read of the dscl man page looking at the readall option:

Displays all the records of a given type. The output of readall is formatted in
the same way as read with a "-" on a line as a delimeter between records.

You can change the way awk treats records. This will print out records where the UniqueID is greater or equal to 500:

awk -v RS="-" '{ for (i=1; i<=NF; i++) if ($i ~ /UniqueID:/ && $(i+1) >= 500) print $0 }'

Do you have an expected output format?

share|improve this answer
    
This is great, just two questions though. Is there more information regarding each record that is not included in your code if the UID >= 500? I'll take every amount of information I can get. Second, this might be too much to ask, but could this be printed out in an xml format? That would be ideal. Otherwise, thank you for the help. –  John Oct 13 '12 at 10:59
    
@Ali: I'm not completely sure I understand your first question. The code above will print out a block if it finds the word 'UniqueID' followed by a value greater or equal to 500. This block is a record and will include all the information it contains (this amount of information may change from record to record or it may not: I'm not familiar with dscl output). Regarding the second question; although awk is not ideal for reading xml, it can be used to write it. Please edit your question to include a good amount of sample data and expected output. –  Steve Oct 13 '12 at 12:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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