sed

addle_brains

New Member
Anybody actually understand how to use sed? I need to create a script that will output the user name, user ID, group ID and home directory of each user in the /etc/passwd file. I've been trying for a few hours but i'm getting lost amongst the delimiting ( with : ) and all the rest.
 
I have always used sed as a find and replace tool. I would use awk command to grab the information your looking for.
EX. cat /etc/passwd | awk -F":" '{printf("User name %s UserID %s GroupID %s HomeDir %s.\n", $1, $3, $4, $6)}'
Or something like that. I'm not around a Linux computer to test.
http://www.ss64.com/bash/gawk.html
 
Thanks, i'd like to avoid using awk. seeing as the passwd file is uses colon delimiting, i didn't think it would be too hard to grab each element and then only output 1, 3, 4, 6. I can't figure it out though.
 
Hey, I managed to do it with just sed:

cat /etc/passwd | sed "s/\(.*\):\(.*\):\(.*\):\(.*\):\(.*\):\(.*\):\(.*\)/\1 \3 \4 \6/"

only problem is formatting the output... if i add tabstops in there, it works, except for a few user names that are too long the formatting goes askew. Is there are way to fix this?
 
Hehe. The confusing part is esaping (\) the parenthesis. You tend to get lost with so many characters in there. Not to worry, I think the stdout formatting is out of my league (i can do it in c++, but that's overkill for this kind of thing).

Thanks for your help.
 
Back
Top