LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2003, 09:03 AM   #1
mimf
LQ Newbie
 
Registered: Dec 2003
Posts: 8

Rep: Reputation: 0
Smile Programming in Perl


I am trying to search the passwd file for a specific gid, and then dump those results into a file. I have almost all of the code, except i can't seem to get only those that match. Can someone please help?
 
Old 12-03-2003, 09:18 AM   #2
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Hi mimf,

Why don't you post what you have. While you're waiting for a reply you could look up "match" under "man perlop". Hit "/" then type "match", use "n" to go forward.

John
 
Old 12-03-2003, 10:10 AM   #3
mimf
LQ Newbie
 
Registered: Dec 2003
Posts: 8

Original Poster
Rep: Reputation: 0
ok, here's that part that i have so far:

#!/usr/bin/perl

open(PASSWD, '/etc/passwd');

print "Enter the group id you want to search for:\n";
$group = <STDIN>;

if($group =~ /[0-9]+/)
{ while (<PASSWD>) {
chomp;
($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split (/:/);
if($gid = $group) #This is where i get stuck
{ while (<PASSWD>) {
chomp;
($login, $passwd, $uid, $gid,
$gcos, $home, $shell) = split (/:/);
print "Enter the filename where you want to
see the results:\n";
$filename = <STDIN>;
{open(MYFILE, ">>$filename");
print MYFILE "$gid $login\n";
close(MYFILE);}}}}}

But its not matching the right gid's and placing them in the file. Again, thanks for any help i can get.
 
Old 12-03-2003, 10:40 AM   #4
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Hi mimf,

It's a classic error (I still make it myself).

"$gid = $group" means set the variable gid to the same value as the variable group. The operation succeeds (why wouldn't it) and returns the value true. Which means that line is the same thing as "if (1)".

Double equals signs, "==", means compare numbers.

I didn't spend much time looking at the rest of the script but am not sure you really want to have another "while (<PASSWD>)" loop inside the first one, and you almost certainly don't want to be asking for the output filename in any loop.

Good luck finishing off,

John

Last edited by jkobrien; 12-03-2003 at 11:40 AM.
 
Old 12-03-2003, 12:31 PM   #5
mimf
LQ Newbie
 
Registered: Dec 2003
Posts: 8

Original Poster
Rep: Reputation: 0
John,

The reason why i was asking for the filename inside the while loop is because i wanted to make sure the $group requested matched that $gid before asking for the filename. Otherwise it would ask for the filename before checking if the $group requested was even valid. Although it makes sense what you said because when i run it, it kept asking for a filename hehe. I appreciate your help.

Mario
 
Old 12-04-2003, 04:58 AM   #6
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Hi Mario,

If you don't want to bother the user for a filename when the search term wasn't found, you could save the hit to some arbitrary filename and ask the user for a new name at the end of the loop, then rename the file to that.

John
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Programming! emailssent Programming 3 06-05-2005 02:25 AM
New to perl programming jester_69 Programming 2 03-17-2005 02:45 PM
Need help with perl (cgi) programming elmetald00d Programming 4 03-28-2002 11:15 PM
Perl programming question LindsayJ Programming 0 11-11-2001 02:58 PM
Perl programming AliceC Programming 4 05-17-2001 10:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration