arrays - capture column and print to file in perl -
i have array list of group ids. i'll use array put id in proprietary linux command using foreach loop, use array elements name files (each id needs output own seperate file). im having issues, opening file , either using awk find , print columns or have tried split command cannot working either. here's sample of have far:
#!/usr/bin/perl -w # title: groupmem_pull.pl # pupose: pull group membership group ids in array use strict; use warnings; $gpath = "/home/user/output"; @grouparray = ( "219", "226", "345", "12", ); print "checking groups:\n"; foreach (@grouparray) { print `sudo st-admin show-group-config --group $_ | egrep '(group id|group name)'`; print `sudo st-admin show-members --group $_ > "$gpath/$_.txt"`; #print `cat $gpath/$_`; #print `cat $gpath/$_ | awk -f"|" '{print $2}' >`; open (file, "$gpath/$_.txt") || die "can't open file\n: $!"; while(my $groupid = <file>) { `awk -f"|" '{print $2}' "$gpath/$_.txt" > "$gpath/$_.txt"`; #print `cat "$gpath/$_.txt" | awk -f"|" '{print $2}' > $_.txt`; } right erring on awk piece saying "use of uninitialized value $2 in concatenation (.) or string @ ./groupmem_pull.pl line 57, line 2." output first commands puts every group id pull in text file seperated pipes. im having hell of time 1 , im not able of samples ive found on stackoverflow work. appreciated!
i think antonh right error message. however, think it's possible result of program not expect. agree maybe "pure perl" solution might work better if eliminate awk component.
if understand correctly, want run command each group in @grouparray.
sudo st-admin show-members --group <group id> from there, read second column, delimited pipe character, , output values in column file named <group>.txt in $gpath folder.
if that's case, think work.
use strict; use warnings; $gpath = "/home/user/output"; @grouparray = qw(219 226 345 12); print "checking groups:\n"; foreach (@grouparray) { open $file, '-|', qq{sudo st-admin show-members --group $_} or die $!; open $out, '>', "$gpath/$_.txt" or die $!; while (<$file>) { # chomp; # if there 2 fields ($field) = (split /\|/, $_, 3)[1]; print $out $field, "\n"; } close $out; close $file; }
Comments
Post a Comment