You can use [man]parse_url/man to get the query string and the path. You then use [man]parse_str/man on the query string and see if there is a name/value pair for which the name is 'gid'. If not, you turn your attention to the path, extracting from it the last component, perhaps by using a regular expression or [man]explode/man.