Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Perl Script that will split /etc/group file.

hcalhounhcalhoun Member Posts: 1
Need help writing a Perl Script that will open and split the etc/group
file. Send find to an array break field down to last field and show
output this is what I have so far.

#!/usr/bin/perl -w

System("cat /etc/group > ./event");
open(DOOR1, "< event") or die ("can not open event);

@ARGV;
if(@ARGV == 0) or die ("empty");

@record = event;
while($record)
{
if (/^c $ARGV[0]/)
{
@record = $_;
}
}
@record split(':', $_);
$allids = $X[3];
@allusers = split(',' allids);

$allids = pop(@allusers);
close(DOOR1);

thanks!
[email protected]

Comments

  • XfactorXfactor Member Posts: 343
    : Need help writing a Perl Script that will open and split the etc/group
    : file. Send find to an array break field down to last field and show
    : output this is what I have so far.
    :
    : #!/usr/bin/perl -w
    :
    : System("cat /etc/group > ./event");
    : open(DOOR1, "< event") or die ("can not open event);

    I'm not sure why you're doing this. That doesn't mean it's wrong.

    : @ARGV;
    : if(@ARGV == 0) or die ("empty");
    :
    : @record = event;
    Does this actually produce data in @record?

    : while($record)
    : {
    : if (/^c $ARGV[0]/)
    : {
    : @record = $_;
    : }
    : }
    : @record split(':', $_);
    : $allids = $X[3];
    : @allusers = split(',' allids);
    :
    : $allids = pop(@allusers);
    : close(DOOR1);
    :
    : thanks!
    : [email protected]

    Here is what I would do:

    open(DOOR, "/etc/group") or die "Can't open /etc/group";
    while () {
    ($group, $x, $gid, $users) = split(':', $_);
    }
    close(DOOR);

    This will get most of it. $users will have comma delimited data but I'm sure you'll be able to get that.
    I haven't tried this, I just wrote it off the top of my head but it's pretty close.

    X
  • JonathanJonathan Member Posts: 2,914
    : : Need help writing a Perl Script that will open and split the etc/group
    : : file. Send find to an array break field down to last field and show
    : : output this is what I have so far.
    : :
    : : #!/usr/bin/perl -w
    : :
    : : System("cat /etc/group > ./event");
    : : open(DOOR1, "< event") or die ("can not open event);
    :
    : I'm not sure why you're doing this. That doesn't mean it's wrong.
    A nicer way to grab the input is:-

    my @records = `cat /etc/group`;

    Remember though that there will be a
    on end of each entry. Then to iterate through them use:-

    [code]foreach (@records) {
    chop;
    # As X said...
    }[/code]

    Alternatively...

    [code]my $records = `cat /etc/group`;
    foreach (split(/
    /, $records)) {
    # As X said...
    }[/code]

    : : @ARGV;
    : : if(@ARGV == 0) or die ("empty");
    : :
    : : @record = event;
    : Does this actually produce data in @record?
    If it does, I've never seen it before. I think maybe the diamond operator should have been used here, but then you need the file handle, not the name of the file. Using my backticks method you don't have to mess around with an intermediate file.

    : : while($record)
    : : {
    : : if (/^c $ARGV[0]/)
    : : {
    : : @record = $_;
    : : }
    : : }
    : : @record split(':', $_);
    : : $allids = $X[3];
    : : @allusers = split(',' allids);
    : :
    : : $allids = pop(@allusers);
    : : close(DOOR1);
    : :
    : : thanks!
    : : [email protected]
    :
    : Here is what I would do:
    :
    : open(DOOR, "/etc/group") or die "Can't open /etc/group";
    : while () {
    : ($group, $x, $gid, $users) = split(':', $_);
    : }
    : close(DOOR);
    :
    : This will get most of it. $users will have comma delimited data but
    : I'm sure you'll be able to get that.
    : I haven't tried this, I just wrote it off the top of my head but
    : it's pretty close.

    Looks pretty OK to me. If you want to split the users on a comma as well then you can always do:-

    ($group, $x, $gid, @users) = split(/[:,]/, $_);

    Which will collect users into the @users array (hopefully).

    So to sumarize my suggestion...

    [code]my $records = `cat /etc/group`;
    foreach (split(/
    /, $records)) {
    #If you have the if conditional, but it around the code
    #below I suspect...
    ($group, $x, $gid, @users) = split(/[:,]/, $_);
    # Do whatever you want with @users...
    }

    #Finish off doing whatever you want...
    [/code]

    Hope this helps,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • JonathanJonathan Member Posts: 2,914
    : : open(DOOR, "/etc/group") or die "Can't open /etc/group";
    : : while () {
    : : ($group, $x, $gid, $users) = split(':', $_);
    : : }
    : : close(DOOR);
    Just noticed, X, you opened the groups file directly which is even cleaner than messing around with backticks and stuff like I suggested. So that's the way to go. So ignore all my suggestions apart from doing the chop (or better, chomp) before the split to get rid of any trailing
    and then you may want to consider my alternative split.

    Sorry 'bout that,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.