Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk to sed equivalent

Status
Not open for further replies.

paublo

ISP
Sep 14, 2006
127
US
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.
 
This might work...

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

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


 
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.


thanks!!

paul
 
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.

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

[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. :D

[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.

[tt]s/:.*$//g[/tt]


 
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.


paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top