There is a text file:

"CN = Username Alibabaev, OU = ?????? ????????????, OU = ??? ep. ????????, OU = ??? \" ???????? ?????? \ ", DC = domain, DC = ru"

"CN = SUsername DAlibabaev, OU = ?????? ????????????, OU = ??? ep. ????????, OU = ??? \" ???????? ?????? \ ", DC = domain, DC = ru"

"CN = Supeman Batman, OU = ??, OU = 22 ????????, DC = domain, DC = ru"

"CN = Sergio Amanol, OU = ??, OU = 22 ????????, DC = domain, DC = ru"

"CN = username, CN = Users, DC = domain, DC = ru"

You need to delete what comes after the "CN=User Suser , on all lines. How can I do this?

  • 2
    so linux or powershell? The answer may be quite different. - andy.37
  • More detail please. For the specified subtool "CN=User Suser I do not observe in the example file shown. - Vitalts
  • And what to do with the last line? Leave CN=... again or delete? - sercxjo

2 answers 2

On PowerShell is also elementary:

 cat filename.txt | %{$_.split(',')[0]} 

If you need to get only user names without CN= , then:

 cat filename.txt | %{$_.split('=,')[1]} 

cat is an alias for Get-Content , % is an alias for Foreach-Object (for processing each line).

    In linux, the problem is solved using awk elementarily (assuming that there is no comma in the CN)

     cat filename.txt | awk -F, '{ print $1; }' 

    -F, - the awk option specifies the delimiter (in this case , default is a space or a group of whitespace characters), then we display the first field. Problems will arise when creating a user named Василий, Алибабаевич

    Since I don’t know powershell at all, I would have solved it using python or perl on Windows, since the benefits are installed quite simply.

    In general, the problem of a comma in the name is also easily solved, if you need to write a script in perl, there will literally be a couple of lines.

    • one
      On Windows, you can put cygwin and solve it in the same way) - Nick Volynkin
    • You are just wizards) Thank you all - user3851609