Awk Question

Kevin Cozens kevin-4dS5u2o1hCn3fQ9qLvQP4Q at public.gmane.org
Mon Oct 29 20:24:05 UTC 2012


On 12-10-29 01:44 PM, William Weaver wrote:
> So I'm still working on my awk skills. I'm trying to write a script that
> allows me to pase a csv file of user rights and get all users that have a
> specific right. Then using that list of users find all other rights those
> users have.

As a rule I use awk where I need to process individual lines of a file to 
alter the output somehow or extract info from each line. For what you are 
suggesting I would probably lean towards using Perl. Depends whether you are 
trying to learn awk or have restrictions preventing use of Perl.

> UserName,Right
> Bob,Add
> Ben,Add
> Ben,Delete
> Tommy,Add
> Sarah,Delete
> Sarah,Edit
>
> My result set if I search for users with Delete should look like
>
> Ben,Delete
> Sarah,Edit

I think you have an error in the example. Are you trying to format the 
output in to "name,list of perms" (ie. Ben,Add,Delete)?

You may be better off parsing the input and building associative arrays. One 
array for each user with their list of permissions. You can then print the 
output of the arrays with the perms for each user that can be piped to a 
file for later searching. Alternatively, you can build arrays based on the 
perms and get a list of names who have a given permission.

You also need to watch out for CSV fields enclosed in quotes where there 
might be a comma between the quotes. It will mess up splitting the CSV input 
in to fields.

> /Delete/ { Users[$1]=$2 }
> /Delete/ {print $1, " - ", Users[$1]}

/Delete/ { Users[$1]=$2; print $1, " - ", Users[$1]; }
or
/Delete/ {
   Users[$1]=$2;
   print $1, " - ", Users[$1];
}

You can also do the print statement as:
   printf "%s - %s",$1, Users[$1];


-- 
Cheers!

Kevin.

http://www.ve3syb.ca/           |"Nerds make the shiny things that distract
Owner of Elecraft K2 #2172      | the mouth-breathers, and that's why we're
                                 | powerful!"
#include <disclaimer/favourite> |             --Chris Hardwick
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list