Extracting a part of String using grep/sed

前端 未结 5 1094
情话喂你
情话喂你 2021-01-18 12:23

I have a file in linux with similar entries as below

dn: CN=HP_NetworkSupport,OU=groups,DC=HDFCSLDM,DC=COM
dn: CN=Review users,OU=groups,DC=HDFCSLDM,DC=COM
<         


        
5条回答
  •  有刺的猬
    2021-01-18 12:25

    This is one way with lookahead:

    grep -Po '(?<=CN=)[^,]*' file > new_file
    

    It gets all text from CN= (not included) until it finds a comma ,. The idea of [^,]* is to fetch any character that is not a comma.

    Test

    $ grep -Po '(?<=CN=)[^,]*' file
    HP_NetworkSupport
    Review users
    

提交回复
热议问题