Sam, impressive, i got as far as sed -e 's/[^:]*:[^:]*:\([^:]*\).*/ \1/' /etc/passwd which basically just displays the UID. I however see what you did, you didnt use logic just got rid of what we didnt need.
After doing some research on sed it seems like awk more powerful and simple to use, is that your thought?
Now im going to have to decipher your sed command.
Yeah, spend the time to learn awk. Sed is great to know, but awk is much better at conditional logic, which is a big part of what you wanted to do.
Actually, time spent learning both of these tools is well worth the effort. They both do amazing things and work very well with each other to fill in the gaps of the other tool. They are very complimentary.
Here's how mine worked (in a spoiler tag so you can figure it out for yourself first)...
The pattern: [tt]'/:x:[0-4][0-9]\{0,2\}:/d; /:x:[0-9][0-9]\{0,1\}:/d; s/:.*$//g'[/tt]
The Explanation...
[tt];[/tt] = A semicolon separates multiple edits. There are three edits given.
[tt]/<pattern>/d[/tt] = If this <pattern> is found in the line, delete (don't display) the line.
[tt]:x:[/tt] = Most modern Unixes use a shadow file for the password, so this field is almost always an 'x'. This is just a hard coded shortcut to find the third field. Your way of coding to find each colon works. I was just being lazy.
[tt][0-4][0-9]\{0,2\}[/tt] = a leading digit of 0 through 4, followed by zero, one, or two numeric digits. So up to 499.
So this, '[tt]/:x:[0-4][0-9]\{0,2\}:/d[/tt]' means to delete any lines with '[tt]:x:0:[/tt]' to '[tt]:x:499:[/tt]' in it. That's fine but it misses anything starting with a 5 though 9, such as '[tt]:x:5[/tt]' through '[tt]:x:9:[/tt]' and '[tt]:x:50:[/tt]' through '[tt]:x:99:[/tt]'. So this deletes those...
[tt]/:x:[0-9][0-9]\{0,1\}:/d[/tt]
Anything that hasn't been deleted at this point must have a UID over 499 and we just need the username, so just chop the first colon and everything after it.
yeah After looking at it i figured it out for myself what you did and came to same breakdown as you did. It's very clever, i guess once you look at its simple but putting the logic together is the tricky part when you dont see sed all the time. I will look at awk also so im more rounded on both.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.