Filter groups

1,183 views
Skip to first unread message

Andrea

unread,
May 30, 2018, 8:59:31 AM5/30/18
to GAM for G Suite
Hi all.
I need to list all groups that name (or description) match a string .

A command like this:
gam print groups where name (or description) like "RED" 

It's possible with GAM ?


I'm not able (I'm a little goat :)

Please, help me.
Thank you all !!
Andrea


IMPORTANT:
This is a business and not a personal e-mail, this message and its attachments are confidential and may also be legally privileged.
If you are not the intended recipient, or have received this e-mail in error, please notify immediately the sender and delete this message and all its attachments.

Any unauthorized review, copying, disclosure, dissemination, or distribution of this message and/or its attachments is strictly forbidden.



Jay Lee

unread,
May 30, 2018, 9:01:38 AM5/30/18
to google-ap...@googlegroups.com
That level of filtering isn't available in the API or GAM but could easily be done in a Sheet:

gam print groups to drive

And then filter to your hearts content in the Google Spreadsheet.

Jay


--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/79ab499c-aa79-4b4a-ad5e-c61a35e68135%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrea

unread,
May 30, 2018, 9:08:49 AM5/30/18
to GAM for G Suite
Thanks Jay !!!
You have been very kind

I will look for an alternative way

Good day
Andrea


Il giorno mercoledì 30 maggio 2018 15:01:38 UTC+2, Jay Lee ha scritto:
That level of filtering isn't available in the API or GAM but could easily be done in a Sheet:

gam print groups to drive

And then filter to your hearts content in the Google Spreadsheet.

Jay


On Wed, May 30, 2018, 8:59 AM Andrea <andrea....@consultant.solgroup.com> wrote:
Hi all.
I need to list all groups that name (or description) match a string .

A command like this:
gam print groups where name (or description) like "RED" 

It's possible with GAM ?


I'm not able (I'm a little goat :)

Please, help me.
Thank you all !!
Andrea


IMPORTANT:
This is a business and not a personal e-mail, this message and its attachments are confidential and may also be legally privileged.
If you are not the intended recipient, or have received this e-mail in error, please notify immediately the sender and delete this message and all its attachments.

Any unauthorized review, copying, disclosure, dissemination, or distribution of this message and/or its attachments is strictly forbidden.



--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsub...@googlegroups.com.

+KimNilsson

unread,
May 30, 2018, 11:01:38 AM5/30/18
to GAM for G Suite
You could also use command line tools like grep when printing the groups. If you're on a system where such tools exist or can be installed.

gam print groups | grep "RED" > List_of_groups_containing_the_word_RED.txt

grep will grab the entire line from the print, so the resulting textfile would have all the content from each line.

However, it will not include the headers, unless one of the headers has RED in it (there isn't one, trust me).
If you run the print command without grep once and almost immediately press Ctrl-C, then GAM will start printing all the groups, including the headers.
Copy that first line, and insert it in the List_of_groups_containing_the_word_RED.txt file after grabbing only those RED-groups.

Oran Dobbins

unread,
May 30, 2018, 11:49:54 AM5/30/18
to GAM for G Suite
I was able to accomplish this by writing a java application to format my CSV.

It is important to use a CSV file that has only the fields you will be working with before you import it into your program environment.

For Java the main tools will needed will be:

BufferedReader 
BufferedWriter

The idea is that you will read in the CSV line by line and then write a new CSV file with only the lines that contain the information you are looking for which in this case is "Red".

It is very important that you create some sort of validation to ensure that you are not getting false positives. This occurs when the word "Red" is in places such as a persons last name which might be in your data set.
So you might have to look at the CSV file and get creative or if you are lucky it will come in the form of  "Last Name, Red" so instead of searching for "Red" you would print only the lines that had ", Red" which no last name should have and therefore will result in only the data you want. Once the file has been formatted the way that you want it then you can use a batch file with gam commands to tie it all up and use the new formatted CSV to execute some intended process.

I hope this helps, and special thanks to Ross Scroggs who helped me get to this solution.

+KimNilsson

unread,
May 30, 2018, 12:06:19 PM5/30/18
to GAM for G Suite
Did you publish your java code somewhere?

Oran Dobbins

unread,
May 30, 2018, 12:55:30 PM5/30/18
to GAM for G Suite
Feel free to use and mod it as you see fit.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class Formatter {
 
 
public static void main(String[] args) {
 
try {
 
/*import CSV files created with GAM Query
 * Contents of CSV file when imported:
 * primaryEmail,customSchemas.Test.Test (Headers Important to include first line)
 * User...@email.com, (Values in the format primaryEmail,Schema)
 * User...@email.com,Staff (Line I want to remove from CSV file)
 * User...@email.com, (Line I want to keep in CSV to update)
 *
 * Desired output from CSV:
 * primaryEmail,customSchemas.Test.Test (Headers Important to include first line)
 * User...@email.com, (Values in the format primaryEmail,Schema)
 * User...@email.com, (Line I want to keep in CSV to update)
 *
 * IO Exception (OR) 'Execution Complete' (Shown in Interface Console [command prompt])
 * */

 
File read1 = new File("CSV File Path IE: 'C:\\file.csv"); //I am formatting two files, 1 and 2.
 
File read2 = new File("C:\\Staff.csv"); //Example
 
File write1 = new File("CSV File Path for the new CSV File"); //This is the file the app is creating
 
File write2 = new File("C:\\StaffFormatted.csv"); //Example
 
String lineToRemove1 = ",Customer"; //Values to be searched and removed
 
String lineToRemove2 = ",Staff";
 
String currentLine;
 
 
 
FileReader fileReader = new FileReader(read1);
 
BufferedReader reader = new BufferedReader(fileReader);
 
BufferedWriter writer = new BufferedWriter(new FileWriter(write1));
 


 


 
while((currentLine = reader.readLine()) != null) {
     
// trim newline when comparing with lineToRemove
     
String trimmedLine = currentLine.trim();
     
//If the code finds ",Customer" it will not write it to the file thus omitting the entire line
     
if(trimmedLine.contains(lineToRemove1)) continue;
     writer
.write(currentLine + System.getProperty("line.separator"));
 
}
 writer
.close();
 
 
//Same as the above I formatted two files in this application.
 fileReader
= new FileReader(read2);
 reader
= new BufferedReader(fileReader);
 writer
= new BufferedWriter(new FileWriter(write2));
 
 
while((currentLine = reader.readLine()) != null) {
     
// trim newline when comparing with lineToRemove
     
String trimmedLine = currentLine.trim();
     
if(trimmedLine.contains(lineToRemove2)) continue;
     writer
.write(currentLine + System.getProperty("line.separator"));
 
}
 
 writer
.close();
 fileReader
.close();


 
System.out.println("Execution complete"); //If app gets to this point print to console 'Execution Complete'
 
} catch (IOException  e) {
 e
.printStackTrace(); //If app fails print stack trace (Error path to console)
 
}
 
}
}

Ross Scroggs

unread,
May 30, 2018, 2:01:12 PM5/30/18
to google-ap...@googlegroups.com
Andrea/Oran/KIm,.


The Google API supports simple exact match and prefix filtering, see: https://developers.google.com/admin-sdk/directory/v1/guides/search-groups
gam print groups query "email:RED*"

If you're doing prefix matching, you must specify at least three characters before the *.

Gam supports more complex filtering, see: https://github.com/taers232c/GAMADV-XTD/wiki/Groups
In these cases, Gam has to download all of the groups and then apply its filters.
gam print groups emailmatchpattern "RED.*Sales.*"

The two filters can be combined to improve performance; the API does the coarse filtering and Gam does the fine filtering.

gam print groups query "email:RED*" emailmatchpattern "RED.*Sales.*"

Ross

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.

To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

For more options, visit https://groups.google.com/d/optout.


--

+KimNilsson

unread,
May 31, 2018, 3:07:42 AM5/31/18
to GAM for G Suite
Thanks, Ross.

But, what if the query must be done on the group name, or other field, and not necessarily in the beginning?
Just do?

gam print groups query "description:*RED*" 

Andrea

unread,
May 31, 2018, 4:25:12 AM5/31/18
to GAM for G Suite
Thanks you all !!!
I really appreciate your help !!!

I'm near the solution.... but..... another little help (sorry!)

I'm using gamadv-xtd-46 client.

This command work
gam print groups namematchpattern "supe...@mydomain.com*"

This (with asterisk at the beginning of the string) NOT work
gam print groups namematchpattern "*supe...@mydomain.com"

Command: gam print groups namematchpattern >>>*supe...@mydomain.com<<<

ERROR: REPattern error: nothing to repeat

I need to match the end of string (like *MYSTRING )

I can I do it ?
It's possible ?

Thank you and good day.
Ciao a tutti !
Andrea





Il giorno mercoledì 30 maggio 2018 15:01:38 UTC+2, Jay Lee ha scritto:
That level of filtering isn't available in the API or GAM but could easily be done in a Sheet:

gam print groups to drive

And then filter to your hearts content in the Google Spreadsheet.

Jay


On Wed, May 30, 2018, 8:59 AM Andrea <andrea....@consultant.solgroup.com> wrote:
Hi all.
I need to list all groups that name (or description) match a string .

A command like this:
gam print groups where name (or description) like "RED" 

It's possible with GAM ?


I'm not able (I'm a little goat :)

Please, help me.
Thank you all !!
Andrea


IMPORTANT:
This is a business and not a personal e-mail, this message and its attachments are confidential and may also be legally privileged.
If you are not the intended recipient, or have received this e-mail in error, please notify immediately the sender and delete this message and all its attachments.

Any unauthorized review, copying, disclosure, dissemination, or distribution of this message and/or its attachments is strictly forbidden.



--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsub...@googlegroups.com.

Ross Scroggs

unread,
May 31, 2018, 9:27:02 AM5/31/18
to google-ap...@googlegroups.com
Andrea,

In Python regular expressions * indicates zero or more repetitions of the prior object, thus it can't appear alone at the beginning by itself; you want .* which means zero or more repitions of anything.

This: gam print groups namematchpattern "supe...@mydomain.com*"
matches supe...@mydomain.com(mmmm...)
You probably want: gam print groups namematchpattern "supe...@mydomain.com.*"

This: gam print groups namematchpattern "*supe...@mydomain.com"
should be: gam print groups namematchpattern ".*supe...@mydomain.com"

To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.

To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

Andrea

unread,
May 31, 2018, 9:28:00 AM5/31/18
to GAM for G Suite
Hello all.
I found the way to match the end of string

the sintax is
gam print groups namematchpattern ".*STRINGtoMATCH"

Just add . before *

Grazie e good job
Andrea

Andrea

unread,
May 31, 2018, 9:34:00 AM5/31/18
to GAM for G Suite
Hello Ros.

Thank you very very much.
I read your answer after publishing mine, sorry :)

I confirm that we need to put the point before the asterisk :)
Thank you all for the exhaustive answers provided for free.

Andrea from 

Ross Scroggs

unread,
May 31, 2018, 9:37:05 AM5/31/18
to google-ap...@googlegroups.com
Kim,

The API: https://developers.google.com/admin-sdk/directory/v1/guides/search-groups deals with fields email and name, not description.
In spite of the fact that the documentation states that this is prefix matching, query "name:test*" will match groups with test anywhere in the name.
The documentation is correct about prefix matching for email addresses, query "email:test* will only match email addresses that begin with test.

The emailmatchpattern and namematchpattern option are full regular expressions matches.

I can add descriptionmatchpattern, the API doesnot support query "description:test*"

Ross
--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

+KimNilsson

unread,
May 31, 2018, 11:32:46 AM5/31/18
to GAM for G Suite
I only said description as an example. But, it might be the next relevant field besides email and name?

Ross Scroggs

unread,
May 31, 2018, 11:41:31 AM5/31/18
to google-ap...@googlegroups.com
Kim,

I can add descriptionmatchpattern, but query "description:{PREFIX}*" is a Google enhancement request.

Ross

On Thu, May 31, 2018 at 8:32 AM +KimNilsson <there.is.no...@gmail.com> wrote:
I only said description as an example. But, it might be the next relevant field besides email and name?

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

For more options, visit https://groups.google.com/d/optout.


--

Kim Nilsson

unread,
May 31, 2018, 11:44:05 AM5/31/18
to Google Apps Manager
Yeah, understood.
At your leisure, my friend. :-) I have, so far, not had the need to query for descriptions.
Honestly, I don't think all that many groups of mine have one.
Reply all
Reply to author
Forward
0 new messages