regexmacosbashdistinguishednamedscl

Regular Expression to parse Common Name from Distinguished Name


I am attempting to parse (with sed) just First Last from the following DN(s) returned by the DSCL command in OSX terminal bash environment...

CN=First Last,OU=PCS,OU=guests,DC=domain,DC=edu

I have tried multiple regexs from this site and others with questions very close to what I wanted... mainly this question... I have tried following the advice to the best of my ability (I don't necessarily consider myself a newbie...but definitely a newbie to regex..)

DSCL returns a list of DNs, and I would like to only have First Last printed to a text file. I have attempted using sed, but I can't seem to get the correct function. I am open to other commands to parse the output. Every line begins with CN= and then there is a comma between Last and OU=.

Thank you very much for your help!


Solution

  • Using sed:

    sed 's/^CN=\([^,]*\).*/\1/' input_file
    
    ^           matches start of line 
    CN=         literal string match
    \([^,]*\)   everything until a comma
    .*          rest