ComputerForum.com ComputerForum.com  
TigerDirect
 
Go Back   Computer Forum > Computer Software > Operating Systems

Reply
 
LinkBack Thread Tools Display Modes
Old 10-04-2007, 05:54 PM   #1 (permalink)
Gold Member
 
addle_brains's Avatar
 
Join Date: Feb 2007
Location: Highfields, QLD, Australia
Posts: 304
Default sed

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
addle_brains is offline   Reply With Quote


Old 10-04-2007, 08:21 PM   #2 (permalink)
Diamond Member
 
Camper's Avatar
 
Join Date: Mar 2005
Location: /dev/null
Age: 22
Posts: 1,227
Default

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
Camper is offline   Reply With Quote
Old 10-04-2007, 11:21 PM   #3 (permalink)
Gold Member
 
addle_brains's Avatar
 
Join Date: Feb 2007
Location: Highfields, QLD, Australia
Posts: 304
Default

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
addle_brains is offline   Reply With Quote
Old 10-06-2007, 01:41 AM   #4 (permalink)
Diamond Member
 
Camper's Avatar
 
Join Date: Mar 2005
Location: /dev/null
Age: 22
Posts: 1,227
Default

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.
Camper is offline   Reply With Quote
Old 10-07-2007, 01:17 PM   #5 (permalink)
Gold Member
 
addle_brains's Avatar
 
Join Date: Feb 2007
Location: Highfields, QLD, Australia
Posts: 304
Default

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
addle_brains is offline   Reply With Quote


Old 10-08-2007, 03:47 AM   #6 (permalink)
Diamond Member
 
Camper's Avatar
 
Join Date: Mar 2005
Location: /dev/null
Age: 22
Posts: 1,227
Default

Nice job. As for the rest your jumping out of my league.
__________________
Dell Inspiron 530
Q6600 Quad
3gb RAM
Nvidia 8600 GT
500GB HD
Camper is offline   Reply With Quote
Old 10-08-2007, 02:17 PM   #7 (permalink)
Gold Member
 
addle_brains's Avatar
 
Join Date: Feb 2007
Location: Highfields, QLD, Australia
Posts: 304
Default

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
addle_brains is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +1. The time now is 11:51 AM.


Powered by: vBulletin Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
Copyright © 2002-2007 Computer Forum and Web Design Forum