|
|
#1 (permalink) |
|
Gold Member
![]() Join Date: Feb 2007
Location: Highfields, QLD, Australia
Posts: 304
|
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.
__________________
ASUS P5K Deluxe /Wifi API Intel Core2 Duo E6750@3.37GHz 2 x 1Gig 667 RAM Inno3D iChill Nvidia 8600GTS XStriker3 2 x Seagate 250Gig SATA HD Samsung DVD-RAM Thermaltake Beetle CPU cooler Thermaltake 850watt Toughpower CoolerMaster Mystique Silver Aluminium Case |
|
|
|
|
|
#2 (permalink) |
|
Diamond Member
![]() Join Date: Mar 2005
Location: /dev/null
Age: 22
Posts: 1,227
|
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
__________________
Dell Inspiron 530 Q6600 Quad 3gb RAM Nvidia 8600 GT 500GB HD |
|
|
|
|
|
#3 (permalink) |
|
Gold Member
![]() Join Date: Feb 2007
Location: Highfields, QLD, Australia
Posts: 304
|
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.
__________________
ASUS P5K Deluxe /Wifi API Intel Core2 Duo E6750@3.37GHz 2 x 1Gig 667 RAM Inno3D iChill Nvidia 8600GTS XStriker3 2 x Seagate 250Gig SATA HD Samsung DVD-RAM Thermaltake Beetle CPU cooler Thermaltake 850watt Toughpower CoolerMaster Mystique Silver Aluminium Case |
|
|
|
|
|
#4 (permalink) |
|
Diamond Member
![]() Join Date: Mar 2005
Location: /dev/null
Age: 22
Posts: 1,227
|
The -F":" switch sets awk to a comma delimited.
I tested it on my pc it works like a charm. Edit: Here is a web page that shows how to use sed. Many Examples!!! http://www.grymoire.com/Unix/Sed.html
__________________
Dell Inspiron 530 Q6600 Quad 3gb RAM Nvidia 8600 GT 500GB HD Last edited by Camper; 10-06-2007 at 02:54 AM. |
|
|
|
|
|
#5 (permalink) |
|
Gold Member
![]() Join Date: Feb 2007
Location: Highfields, QLD, Australia
Posts: 304
|
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?
__________________
ASUS P5K Deluxe /Wifi API Intel Core2 Duo E6750@3.37GHz 2 x 1Gig 667 RAM Inno3D iChill Nvidia 8600GTS XStriker3 2 x Seagate 250Gig SATA HD Samsung DVD-RAM Thermaltake Beetle CPU cooler Thermaltake 850watt Toughpower CoolerMaster Mystique Silver Aluminium Case |
|
|
|
|
|
#7 (permalink) |
|
Gold Member
![]() Join Date: Feb 2007
Location: Highfields, QLD, Australia
Posts: 304
|
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.
__________________
ASUS P5K Deluxe /Wifi API Intel Core2 Duo E6750@3.37GHz 2 x 1Gig 667 RAM Inno3D iChill Nvidia 8600GTS XStriker3 2 x Seagate 250Gig SATA HD Samsung DVD-RAM Thermaltake Beetle CPU cooler Thermaltake 850watt Toughpower CoolerMaster Mystique Silver Aluminium Case |
|
|
|