Page 1 of 1
Replace a line start with single space
Posted: Fri Dec 23, 2011 1:36 pm
by srive99
I have following lines in ldap format
cn=some, ou=some, ou=s
ome, o=org
cn=somethingelse, ou=same, o
u=same, o=org
cn=same, ou=some,ou=something,
o=org
I want to do couple of things
1, I want to remove a line that start with space. I'm looking for following output
cn=some, ou=some, ou=s
cn=somethingelse, ou=same, o
cn=same, ou=some,ou=something,
2, I want to join a line start with space to previous line and output should as below
cn=some, ou=some, ou=some,o=org
cn=somethingelse, ou=same, o=org
cn=same, ou=some,ou=something, o=org
Could some one help to do this with regular expression. I need to lot of export, cleans and import the data to ldap.
Thanks
Posted: Fri Dec 23, 2011 6:11 pm
by kengrubb
If I understand your goal correctly, thy these three Regex Replace Alls. The Replace with in the first two is nothing, blank, zip, nada.
Code: Select all
Find what: [[:blank:]]+$
Replace with:
Find what: \n
Replace with:
Find what: (cn[=])
Replace with: \n\1
Posted: Tue Dec 27, 2011 2:19 pm
by srive99
I tried but when I try with second RE, it stuck and never come back. Had to to kill the textpad.
Thanks
Posted: Wed Dec 28, 2011 3:28 am
by kengrubb
OK, let's try this. The first Replace with is nothing, to eliminate trailing blanks.
Code: Select all
Find what: [[:blank:]]+$
Replace with:
Find what: \n([^c][^n][^=])
Replace with: \1
Posted: Wed Dec 28, 2011 11:44 am
by ben_josephs
To show your examples with their original layout, including leading white space, enclose them in
[/color][/b] tags (without the space I've added to stop the tags being interpreted here). I had to examine the source of the page to understand your requirement.
1. To remove each line that starts with a space:
Find what: ^_.*\n [Replace the underscore with a space]
Replace with: [nothing]
[X] Regular expression
Replace All
2. To join each line that starts with space to the line that precedes it:
Find what: \n_ [Replace the underscore with a space]
Replace with: [nothing]
[X] Regular expression
Replace All
[Edit: Error in second regex corrected.]
Posted: Wed Dec 28, 2011 5:23 pm
by srive99
Thanks Ben
First one worked to remove the lines that start with a space but second one did not work to join the lines
that starts with space to the preceding line.
I get the can not find regular expression error. I made sure Regular Expression checkbox selected.
dn: cn=user,ou=test,ou=test,dc=xxxx,
_dc=yyyy,dc=org
memberof: cn=groupone,ou=test,ou=testa,dc=xx
_xx,dc=yyyy,dc=org
memberof: cn=grouptwo,ou=test,ou=testa,dc=xxxx,
_dc=yyyy,dc=org
memberof: cn=groupthree,ou=test,ou=testa,dc=xxxx,d
_c=yyyyy,dc=org
where _ is a space in my file. I need following ouput.
dn: cn=user,ou=test,ou=test,dc=xxxx,dc=yyyy,dc=org
memberof: cn=groupone,ou=test,ou=testa,dc=xxxx,dc=yyyy,dc=org
memberof: cn=grouptwo,ou=test,ou=testa,dc=xxxx,dc=yyyy,dc=org
memberof: cn=groupthree,ou=test,ou=testa,dc=xxxx,dc=yyyyy,dc=org
Thanks again.
Posted: Wed Dec 28, 2011 5:38 pm
by ben_josephs
It does work. Are you using the second replacement as well as the first one? You should be using it instead of the first one. Is there a spurious space at the end of your regex?
Please use code tags, as I suggested, to display leading spaces correctly, not quote tags.
Posted: Wed Dec 28, 2011 6:54 pm
by srive99
I just tried again but no luck. I did not use it with first one as I had to use those on two different files.
As the first one worked, I been working on the second where I need to join the lines with preceding lines.
I'm using following..
Code: Select all
Find what: ^\n- (space instead of -)
Replace with: n/a
Thanks again.
Posted: Wed Dec 28, 2011 7:06 pm
by ben_josephs
I'm sorry. It's my error: a bad edit when constructing my posting. The regex should be
\n_ [Replace the underscore with a space]
I've corrected my original post.
Apologies again.
Posted: Wed Dec 28, 2011 7:19 pm
by srive99
You are the man. That did the trick.
Thanks a bunch.