Operating System - HP-UX
1833490 Members
2730 Online
110052 Solutions
New Discussion

getting data to the end of a line

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

getting data to the end of a line

hello all,

I would like some help on getting all information from a line after a certain string:

# id user

uid= (user) gid= (grp) groups=(grp),(grp),30331(grp),(grp),(grp), etc etc

I want to count how many grps a user is in by using awk so how do I get awk to display all information and including the field groups=(grp).

# awk -F',' '???????? {print NF}'

thanks in advance.

Chris.
hello
9 REPLIES 9
Arturo Galbiati
Esteemed Contributor
Solution

Re: getting data to the end of a line

Hi,
awk -F"groups=" '{n=split($2,a,",")}; END {for (i=1;i<=n;i++) print i "-" a[i]}' f1

this is the result:
salvador/home/galbiati/> cat f1
uid= (user) gid= (grp) groups=(grp1),(grp2),30331(grp3),(grp4),(grp5)

salvador/home/galbiati/> awk -F"groups=" '{n=split($2,a,",")}; END {for (i=1;i<=n;i++) print i "-" a[i]}' f1
1-(grp1)
2-(grp2)
3-30331(grp3)
4-(grp4)
5-(grp5)
salvador/home/galbiati/>

if you want format the output fill free to change the last print.

HTH,
Art


blah2blah
Frequent Advisor

Re: getting data to the end of a line

why not just do it in the shell

iduser=$(id user)
groups="groups${iduser#*groups}"
print $groups
blah2blah
Frequent Advisor

Re: getting data to the end of a line

or just use

id -G, or the groups command
blah2blah
Frequent Advisor

Re: getting data to the end of a line

id user | awk -F"=" '{print $4;}'
Victor BERRIDGE
Honored Contributor

Re: getting data to the end of a line

groups |wc -w
Shoould do...


All the best
Victor
blah2blah
Frequent Advisor

Re: getting data to the end of a line

> I would like some help on getting all information from a line after a certain string:

probably the easy way is to use the shell


${parameter#pattern}
${parameter##pattern}
If the shell pattern matches the beginning of the value of parameter, the value of this substitution is the value of the parameter with the matched portion deleted; otherwise the value of this parameter is substituted. In the former case, the smallest matching pattern is deleted; in the latter case, the largest matching pattern is deleted.

${parameter%pattern}
${parameter%%pattern}
If the shell pattern matches the end of the value of parameter, the value of parameter with the matched part is deleted; otherwise substitute the value of parameter. In the former, the smallest matching pattern is deleted; in the latter, the largest matching pattern is deleted.

Where the form of the patterns is the Pattern Matching Notation defined by regexp(5).

Peter Nikitka
Honored Contributor

Re: getting data to the end of a line

Hi,

if the context does not allow the use of the groups command, try the following. Having only a SUN at hand now, I see
id -a
uid=466(ef3nip00) gid=1710(nsd) groups=1710(nsd),232(audmcumc),1500(m2nck)

and use
id -a | awk '{g=split($3,a,",");print $1,$2,g"_"$3}'

to get the number of groups "3" inserted:
uid=466(ef3nip00) gid=1710(nsd) 3_groups=1710(nsd),232(audmcumc),1500(m2nck)

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Srikanth Arunachalam
Trusted Contributor

Re: getting data to the end of a line

Hi,

The credit goes to Arturo. I simple want to debug and show the beauty of one-liner in 3 -steps.

xyz.abc.com> id
uid=5133(rc_prd) gid=17425(rc_prd) groups=17425(rc_prd),2337(bdlexch),6075(xcollftp),18660(svc_ficd)

xyz.abc.com> id|awk -F "groups=" '{print $2}'

17425(rc_prd),2337(bdlexch),6075(xcollftp),18660(svc_ficd)

xyz.abc.com> id|awk -F"groups=" '{n=split($2,a,",")}; END {for (i=1;i<=n;i++) print i "-" a[i]}'

1-17425(rc_prd)
2-2337(bdlexch)
3-6075(xcollftp)
4-18660(svc_ficd)

Thanks,
Srikanth
lawrenzo_1
Super Advisor

Re: getting data to the end of a line

ok thanks all, some good examples that i can adapt.

Blah2blah - thanks for the detailed functionality.

have a nice weekend

Chris.
hello