how to grab a string

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

how to grab a string

Post by srive99 »

hi all,
I just have an import (more than 50k lines) from ldap directory and I need to grab just usernames. The user name starts with "cn=". I just need this part.

I also need, once I get the CN=usernames from the file, I want to add some context like following....
cn=username, ou=some, o=company.

Can I do that using just textpad?

Thanks for help
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Please give examples of inputs and required outputs.

Where will the ou and o values come from?
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

Thanks for your reply. Here is the input i have...
<modify cached-time="20100412173739.761Z" class-name="User" event-id="pwd-subscribe" qualified-src-dn="O=some\OU=company\CN=I266668" src-dn="\TREE\some\company\I266668" src-entry-id="50207" timestamp="0#0">
from this I need only "cn=*" part.
and also some cases the input is something like this...
cn=some user, ou=some, o=company
I also need get the "cn=some user" (please keep in mind that I have more 50k users in the file and I need to copy all these users(cn=some user and/or cn=somenumber) probably to other file)
once I copy to other file, I must add some dn to user. we can hard code OU and O as that is just one value.
Final out put should be as follows...
dn: cn=some user, ou=some, o=company
if the cn=somenumber output should be as follows...
dn: cn=somenumber, ou=some, o=company
I hope you understand.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

That is not entirely clear.

Does this do what you want?
Find what: .*CN=([a-z0-9 ]+).*
Replace with: dn: cn=\1, ou=some, o=company

[X] Regular expression

Replace All
This assumes you are using Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

OK I just want to copy "CN=someuser" from the input I given in previous, to other file.
or instead of copying to other file, leave the CN=someuser and remove the rest of from input file.

The one which you give me, is just replacing with full DN. I dont need other stuff from input file as I just need CN=someuser.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

My suggestion is an attempt to solve your problem as a whole. You can copy the input file to the output file and run the replacement. Does this not work?

Can you not work out the simpler solution from the one I gave you?
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

I think the replacement will work but the problem is, in the input file there is big difference in the users like below...
<modify cached-time="20100412171458.455Z" class-name="User" event-id="JMS iTIM Driver#8287298363575512505" qualified-src-dn="O=some\ou=company\CN=I269972" src-dn="\TREE\some\company\I269972" src-entry-id="53859" timestamp="0#0">
<modify cached-time="20100412171502.280Z" class-name="User" event-id="pwd-subscribe" qualified-src-dn="O=some\ou=companyCN=U260366" src-dn="\TREE\some\company\\U260366" src-entry-id="43309" timestamp="0#0">
<modify cached-time="20100412171511.079Z" class-name="User" event-id="pwd-subscribe" qualified-src-dn="O=some\ou=company\CN=M002152" src-dn="\TREE\some\company\\M002152" src-entry-id="109849" timestamp="0#0">
<modify cached-time="20100412171526.775Z" class-name="User" event-id="pwd-subscribe" qualified-src-dn="O=some\ou=company\CN=U265165" src-dn="\TREE\some\company\U265165" src-entry-id="47873" timestamp="0#0">
in this case I'm not sure how the replace will work. With the solution you gave me, I can only able to find CN=* value. once I got all CN's in my output file, then I can run replace, i think. so first think I want to have only CN's in the output file.

Thanks.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

You wrote: With the solution you gave me, I can only able to find CN=* value.
Earlier, you wrote: I need only "cn=*" part.
These cannot both be right.

I really don't understand what you want.

Please give examples of inputs and required outputs (as I asked for before).
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

OK here is the input....
<modify cached-time="20100412171458.455Z" class- name="User" event-id="JMS iTIM Driver#8287298363575512505" qualified-src-dn="O=some\ou=company\CN=I269972" src-dn="\TREE\some\company\I269972" src-entry-id="53859" timestamp="0#0">
<modify cached-time="20100412171502.280Z" class-name="User" event-id="pwd-subscribe" qualified-src-dn="O=some\ou=companyCN=U260366" src-dn="\TREE\some\company\\U260366" src-entry-id="43309" timestamp="0#0">
<modify cached-time="20100412171511.079Z" class-name="User" event-id="pwd-subscribe" qualified-src-dn="O=some\ou=company\CN=M002152" src-dn="\TREE\some\company\CN=M002152" src-entry-id="109849" timestamp="0#0">
<modify cached-time="20100412171526.775Z" class-name="User" event-id="pwd-subscribe" qualified-src-dn="O=some\ou=company\CN=U265165" src-dn="\TREE\some\company\CN=U265165" src-entry-id="47873" timestamp="0#0">
This is sample input and I have like 50K lines in my input file. From this file, I need "CN=*" in output file. The output file should look like this...
CN=I269972
CN=U260366
CN=M002152
CN=U265165
Hope you got this time...
Thanks.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

As I said, you can work that out from the solution I gave you:
Find what: .*CN=([a-z0-9 ]+).*
Replace with: cn=\1

[X] Regular expression

Replace All
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

OK thanks that did it as I want.

Now, to this output I need to add full DN. The first output file is like this...
CN=I269972
CN=U260366
CN=M002152
CN=U265165
from this I need following
dn: CN=I269972, ou=some, ou=some, o=company
changetype: modify
add: someattribute
someattribute: values

dn: CN=U260366, ou=some, ou=some, o=company
changetype: modify
add: someattribute
someattribute: values

dn: CN=M002152, ou=some, ou=some, o=company
changetype: modify
add: someattribute
someattribute: values
Thanks again.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Find what: .+
Replace with: dn: \0, ou=some, ou=some, o=company\nchangetype: modify\nadd: someattribute\nsomeattribute: values\n

[X] Regular expression

Replace All
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

It seems to be something. It worked before but not now, Im just copying and pasting you solution.

I just did everything from the beginning and everything works like charm.

but when I try with different file, with the same input it doesn't work.

Thanks
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Which replacement doesn't work?
In what way doesn't it work?
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

all of them. it says cann't find regular expression. Is there any settings that needs to be changed.
i did something
[X] Regular expression

and

Configure | Preferences | Editor

[X] Use POSIX regular expression syntax

Thanks
Post Reply