Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

awk to sed equivalent

awk to sed equivalent

awk to sed equivalent

How would you convert the following awk command to sed, i can't seem to figure it out.

edquota -p bob `awk -F: '$3 > 499 {print $1}' /etc/passwd`

thansk P.

RE: awk to sed equivalent

This might work...


sed '/:x:[0-4][0-9]\{0,2\}:/d; /:x:[0-9][0-9]\{0,1\}:/d; s/:.*$//g' /etc/passwd 

awk is a lot more concise and cleaner though. sed doesn't do conditionals very cleanly.

RE: awk to sed equivalent

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.



RE: awk to sed equivalent

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: '/:x:[0-4][0-9]\{0,2\}:/d; /:x:[0-9][0-9]\{0,1\}:/d; s/:.*$//g'

The Explanation...
; = A semicolon separates multiple edits. There are three edits given.

1) /:x:[0-4][0-9]\{0,2\}:/d
2) /:x:[0-9][0-9]\{0,1\}:/d
3) s/:.*$//g

/<pattern>/d = If this <pattern> is found in the line, delete (don't display) the line.

:x: = 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. :D

[0-4][0-9]\{0,2\} = a leading digit of 0 through 4, followed by zero, one, or two numeric digits. So up to 499.

So this, '/:x:[0-4][0-9]\{0,2\}:/d' means to delete any lines with ':x:0:' to ':x:499:' in it. That's fine but it misses anything starting with a 5 though 9, such as ':x:5' through ':x:9:' and ':x:50:' through ':x:99:'. So this deletes those...


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.


RE: awk to sed equivalent

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.

thanks again sam for the help.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close