Splitting multiple fields of /usr/bin/id

Hi,

Iv got the following input

$id |grep uid
uid=6090(dsiddiq) gid=1(staff) groups=4001(cdgrp)

and Im using the below command to split the field to grab the numberical userid as well the alphabetical userid

$id|awk -F'=' '{print $2}'|awk -F')' '{print $1}'|awk -F'(' '{print $1" "$2}'
6090 dsiddiq

I wanna know is there a shorter way to to achieve the same result

I am on an AIX machine

How about

id | awk '{gsub (/^.*=|\)/, "", $1); sub (/\(/, " ", $1); print $1}' 
1000 userid
1 Like

Does id on AIX have the -u and -n switches?

$ id -u;id -un
12345
apm

If not, please ignore.

Andrew

@apmcd47, my AIX doesn't have the flags you suggested, but thanks anyway

hi, couple of questions

/^.*=|

what does this mean in the regex part of gsub?

^.*=|\)/, ""

in the gsub() function are you saying -> replace the occurrence of this regex with nothing or no space. I think you have replaced the occurrence of the regex with a space

" "

in the sub() function

  1. You have used a semicolon to separate gsub() and sub(). Do semi colons work as the same way a pipe (|) does? I ran the gsub and sub separately and from what I could figure out, you are working on the output of gsub in sub().
  1. /.../ is a regular expression; c.f. man awk : "Regular expressions are enclosed in slashes". A single / is pointless.
    ^ anchors the regex at begin-of-line (or string).
    .* represents "any char, zero or more times".
    = terminates this special regex. As we are sure there's only ONE = in $1 , no additional measures need to be taken.
    | is the alternation operator ("or"), so multiple regexes can be matched
  2. Yes, that's why I used two function calls: for different results. If you accept leading and trailing spaces, try gsub (/^.*=|\)|\(/, " ", $1) and drop the sub .
  3. Semicolons separate commands in awk , as do line feeds. gsub modifies $1 , then sub works on that mod'd $1
1 Like