Manipulating the etc/passwd file with sed

How can i use sed to extract the user name and home directory from the /etc/passwd/ file on my server.

A word of warning - do not do anything to /etc/passwd that will edit the file

awk '{ print $1, $6}' /etc/passwd

Ok. I am not editing the file. Is there a way I can do it using Sed?

sed "s/\(.*\):.*:.*:.*:.*:\(.*\):.*/\1 \2/" /etc/passwd

Why do that when awk is so easy???

You're asking some silly questions!

I am a new sys admin. Wanted to know options.

Thnx

sed 's/:[^:]*$//;s/:.*:/ /' /etc/passwd

Thnx. That is easier

Easier than what?!

Easier than

awk '{ print $1, $6}' /etc/passwd

I hardly think so!

---------- Post updated at 12:30 AM ---------- Previous update was at 12:01 AM ----------

My apologies.

There is an error in the original awk:

awk -F: '{ print $1, $6}' /etc/passwd

sed is not the appropriate tool for this job, although it can do it, but the code it produce is ugly and hard to read due to extensive (or abusive) use of regex. Use awk and break the file rows into pieces using delimiters. That's the easiest way it can be.

I agree with ghostdog. In situations where there is a clear field oriented format awk solutions usually provide more straightforward code. I disagree about the supposed ugliness and hard to read regex (and I do not get the abuse bit), but I like regex. :wink:

you can take it that i mean hard to read and forget about the word "ugliness". regex is sure powerful, but too much of it in one place makes your code "hard to read". Its like reading an essay with words encoded in numbers. that's what i meant.

Ofcourse awk is better. Wanted to know how to use sed as an option