0

I got output from command like below. Need to extract group names.

dsAttrTypeNative:memberOf: CN=Grupa_test,OU=Groups,DC=yellow,DC=com CN=Firefox_Install,OU=Groups,DC=yellow,DC=com CN=Network_Admin,OU=Groups,DC=yellow,DC=com

So I would like to have something like:

Grupa_test
Firefox_Install
Network_Admin

Amount of groups will be different each time so I'm not sure how to achieve that.

Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51
Yellow
  • 11
  • 1

4 Answers4

2
$ awk -v RS=' ' -F'[=,]' 'NR>1{print $2}' file
Grupa_test
Firefox_Install
Network_Admin

The above will work with any awk.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

You can do it with GNU grep:

grep -oP '(?<=CN=)[^,]*' file
SLePort
  • 15,211
  • 3
  • 34
  • 44
0

try with following awk too once.

awk -v RS='[ ,]' -v FS="=" '/CN=/{print $2}'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0
$ awk -v FPAT="CN=[^,]+" '{for(i=1;i<=NF;i++)print substr($i,4)}' Input_file

Treat every matched CN=[^,]+ case as a field. And for each matched field, use substr($i,4) to filter out CN=, to print the desired string.

CWLiu
  • 3,913
  • 1
  • 10
  • 14
  • I've modified two things and got exactly what I need 'code' dscl /Active\ Directory/YELLOW/All\ Domains read /Users/tzolty dsAttrTypeNative:memberOf | awk -v FPAT="CN=[^,]+" '{for(i=2;i<=NF;i++)print substr($i,4)}' | awk -F , '{ print $1}' so added awk -F , '{ print $1}' because I need only group name and without that it was giving me something like Grupa_test,OU=Groups,DC=yellow,DC=com Firefox_Install,OU=Groups,DC=yellow,DC=com Network_Admin,OU=Groups,DC=yellow,DC=com After that change I got below: Grupa_test Firefox_Install Network_Admin Thanks a lot !!!! – Yellow Jul 28 '17 at 10:42
  • @Yellow you needed to make that change because the `FPAT` assignment is being ignored and so the first awk script is splitting on spaces by default and so not working for you at all. CWLiu you should mention that your solution requires GNU awk (which the OP apparently is not running). – Ed Morton Jul 28 '17 at 11:16