baurel has asked for the wisdom of the Perl Monks concerning the following question:

hi
another data parsing question. I work on a mac and try to analyse my group IDs

$ sudo dscl . list groups gid
...
_amavisd 83
_appowner 87
_appserveradm 81
_appserverusr 79
_ard 67
_atsserver 97
_calendar 93
_clamav 82
...

the integer values are actually are positioned strictly vertical, the space between the groupname and the ID is filled with spaces. (the forum software removes these spaces)

I guess the solution would be to pipe the output of the command into a file and then loop through every line

$ sudo dscl . list groups gid >> output.txt

and then something like that:

1 #!/usr/bin/perl 2 3 open(FH, "<output.txt"); 4 while(<FH>) 5 { 6 chomp($_); 7 #parse $_ somehow ... 8 print("current line: $_ \n"); 9 } 10 close(FH);


The question now is. What do I have to do on line 7 in order to find the line with a specific ID, say 87? That would be the following line
...
_appowner 87
...

- how can I find that line?
- how can I extract the group name (_appowner)?

any ideas?

thanks in advance
Ben

Replies are listed 'Best First'.
Re: finding a value in a list
by jettero (Monsignor) on Jul 17, 2008 at 16:13 UTC

    next unless m/^(_[\w]+)\s+(87)$/; my ($group,$id) = ($1,$2);

    or next unless m/^(_appowner)\s+(\d+)$/; my ($group,$id) = ($1,$2);

    There's probably a million other ways to do it though.

    -Paul

Re: finding a value in a list
by psini (Deacon) on Jul 17, 2008 at 16:20 UTC

    First of all, you don't really need the output.txt file, you can redirect the output of your command to the perl script as in $ sudo dscl . list groups gid | program.pl and read the data from stdin (while(<>)) without the need of open and close.

    To parse the lines you can use a regex match like this> $_ =~ /^\s*(\S+)\s*(\d+)\s*$/; if the line matches, you'll find the group name in $1 and the ID in $2.

    Updated: corrected a type in the first code line. Thanks to toolic and, no, I don't know if mac is very different from unix, but this keyboard I'm using today is very different from my own! :-)

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: finding a value in a list
by olus (Curate) on Jul 17, 2008 at 16:20 UTC
    the integer values are actually are positioned strictly vertical, the space between the groupname and the ID is filled with spaces. (the forum software removes these spaces)

    If you use <code> tags, like you used in the Perl code, the spaces are preserved.

Re: finding a value in a list
by toolic (Bishop) on Jul 17, 2008 at 17:35 UTC
Re: finding a value in a list
by baurel (Sexton) on Jul 17, 2008 at 21:23 UTC
    thank you all for your super quick and helpful replies ben
Re: finding a value in a list
by Bloodnok (Vicar) on Jul 18, 2008 at 10:31 UTC
    Hi ,

    Why not, assuming the mac has similar piping etc capabilities as *NIX, something along the lines of (untested)...

    sudo dscl . list groups gid | perl -ane "$lookup{$@F[1]} = {$@F[0]}; E +ND{print $lookup{87}}"

    HTH

    At last, a user level that overstates my experience :-))